繁体   English   中英

在Android中配对两个蓝牙设备

[英]pair two bluetooth devices in android

我希望用户在列表中选择的设备配对。 在程序中搜索蓝牙设备,并向用户显示蓝牙设备列表。

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演示应用程序,它将对您有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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