简体   繁体   中英

pair two bluetooth devices in android

I want the device which user selects in list paired. in program search in bluetooth device and show list of bluetooth device to user.

if(ba.isEnabled()){
            if (ba.isDiscovering()) {
                ba.cancelDiscovery();
            }
            ba.startDiscovery();

            final BroadcastReceiver mReceiver = new BroadcastReceiver() {
                public void onReceive(Context context, Intent intent) {
                    String action = intent.getAction();
                    // When discovery finds a device

                    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                        // Get the BluetoothDevice object from the Intent
                        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        // Add the name and address to an array adapter to show in a ListView
                        arr.add(device.getName() + "\n" + device.getAddress());
                        adapter.notifyDataSetChanged();
                        adapter.notifyDataSetInvalidated();
                    }
                }
            };
            // Register the BroadcastReceiver
            IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
            registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
        }

进入sdk> sample> android-7> BluetoothChat。在您的日食中导入BluetoothChat演示应用程序,它将对您有所帮助

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM