簡體   English   中英

尋找Android藍牙配對設備

[英]Finding Android Bluetooth Paired Devices

我正在嘗試創建一個圖像按鈕,當按下該按鈕時,會向用戶顯示要連接的已配對藍牙設備的列表。

但是,我在“## 1”處得到“Set as not resolved as a variable”,並且## 2處的“mArrayAdapber無法解析”(## 1和## 2不是代碼的一部分...)

我使用了Android網站上的代碼,但是在黑暗中,我發現自己處於黑暗中。

我很感激一些指導......

//搜索

ImageButton bSearch = (ImageButton) findViewById(R.id.Search);
bSearch.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {                        
        ##1Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
        // If there are paired devices
        if (pairedDevices.size() > 0) {
            // Loop through paired devices
            for (BluetoothDevice device : pairedDevices) {
                // Add the name and address to an array adapter to show in a ListView
                ##2mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            }
        }                                                                           
    }                       
});

1)如果你還沒有這樣做,請添加

> import java.util.Set;

在您的import語句中。 這將解決“設置”錯誤。

2)聲明並初始化

mArrayAdapter

例如,在您的Activity中:

private ArrayAdapter<String> mArrayAdapter;

然后在onCreate上:

 mArrayAdapter= new ArrayAdapter<String>(this, <your layout file>);

然后應該將其添加到ListView中

//為新發現的設備查找並設置ListView

   ListView newDevicesListView = (ListView)
 findViewById(R.id.<layout_file>);
         newDevicesListView.setAdapter(mArrayAdapter);

 newDevicesListView.setOnItemClickListener(mDeviceClickListener);

請參閱Android示例中的藍牙聊天示例。 它應該可以幫助您使用藍牙API


評論更新:

如果仔細查看BT示例中的BluetoothChat.java文件,您會看到這一點

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(D) Log.d(TAG, "onActivityResult " + resultCode);
        switch (requestCode) {
        case REQUEST_CONNECT_DEVICE:
            // When DeviceListActivity returns with a device to connect
            if (resultCode == Activity.RESULT_OK) {
                // Get the device MAC address
                String address = data.getExtras()
                                     .getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
                // Get the BLuetoothDevice object
                BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
                // Attempt to connect to the device
                mChatService.connect(device);
            }
            break;
        case REQUEST_ENABLE_BT:
            // When the request to enable Bluetooth returns
            if (resultCode == Activity.RESULT_OK) {
                // Bluetooth is now enabled, so set up a chat session
                setupChat();
            } else {
                // User did not enable Bluetooth or an error occured
                Log.d(TAG, "BT not enabled");
                Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    }

看這行:

 // Attempt to connect to the device
 mChatService.connect(device);

此功能連接到藍牙設備。 第一次它會要求你自動配對。 配對后,下次它將自動連接到藍牙設備。

嗨,你也可以嘗試這個代碼,你只需要獲得一套綁定設備。

 private ArrayAdapter<String> bondedAdapter = null;
    private ListView listViewPairedDevices = null;

@Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        try {
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    listViewPairedDevices = (ListView) findViewById(R.id.listViewPairedDevices);
    bondedAdapter = new ArrayAdapter<String>(this, R.layout.lyt_scanlist_textview);

    Set<BluetoothDevice> bondedSet = bluetoothAdapter.getBondedDevices();
                Log.v(BluetoothDemoActivity.LOG, "BluetoothDemo : bondedSet: "+bondedSet);

                int count = 0;
                if(bondedSet.size() > 0){
                    for(BluetoothDevice device : bondedSet){
                        textViewPairedDevice.setVisibility(View.VISIBLE);
                        bondedAdapter.add(device.getName()+"\n"+device.getAddress());
                        Log.v(BluetoothDemoActivity.LOG, " count = "+count++);
                    }
                }else{
                    bondedAdapter.add("No Devices");
                }

    listViewPairedDevices.setAdapter(bondedAdapter);
} catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e(BluetoothDemoActivity.LOG, e.toString(),e.fillInStackTrace());
        }
    }//onStart ends 

只需從代碼中的##1Set<BluetoothDevice>##2mArrayAdapter刪除##1##2 我想你只是從其他來源復制/粘貼而沒有注意。 這不是原始代碼的一部分。 它僅用於列表編號目的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM