How to retrieve Bluetooth device info with Android Bluetooth device picker?

前端 未结 2 1698
谎友^
谎友^ 2021-02-04 19:34

Here is the test code that I am using :

public class IOConnectDirect extends Activity {

private static final String TAG = \"IOConnectDirect\";

private static f         


        
2条回答
  •  时光说笑
    2021-02-04 20:13

    Try this code:

    private final BroadcastReceiver mBluetoothPickerReceiver = new BluetoothConnectActivityReceiver(this);
    
     void connectToService(String defaultAdapter) {
        if (defaultAdapter == null) {
            registerReceiver(mBluetoothPickerReceiver, new IntentFilter(BluetoothDevicePicker.ACTION_DEVICE_SELECTED));
            startActivity(new Intent(BluetoothDevicePicker.ACTION_LAUNCH)
                .putExtra(BluetoothDevicePicker.EXTRA_NEED_AUTH, false)
                .putExtra(BluetoothDevicePicker.EXTRA_FILTER_TYPE, BluetoothDevicePicker.FILTER_TYPE_ALL)
                .setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS));
        } else {
            mCardroid.getCardroidService().connectTo(defaultAdapter);
        }
    }
    
    public class BluetoothConnectActivityReceiver extends BroadcastReceiver {
        private BluetoothConnectActivity bluetoothConnectActivity;
     public BluetoothConnectActivityReceiver(BluetoothConnectActivity bluetoothConnectActivity) {
                this.bluetoothConnectActivity = bluetoothConnectActivity;
            }
    
         @Override
                public void onReceive(Context context, Intent intent)  {
                    if(BluetoothDevicePicker.ACTION_DEVICE_SELECTED.equals(intent.getAction())) {
                        context.unregisterReceiver(this);
                        BluetoothDevice device = (BluetoothDevice) intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        bluetoothConnectActivity.connectToService(device.getAddress());
                    }
                }
            }
    

    The reference of complete code is :

    http://code.google.com/p/carbot/source/browse/trunk/src/net/cardroid/?r=8

提交回复
热议问题