How to pair Bluetooth device programmatically Android

前端 未结 8 1973
不知归路
不知归路 2020-11-27 12:35

I am developing an application where I want to connect a Bluetooth device main issue is I don\'t want user to enter required pin instead application should do that by himsel

8条回答
  •  孤城傲影
    2020-11-27 13:27

    How to set the pin code has been answered above (and that helped me). Yet, I share my simple code below which works with Android 6:

    BluetoothAdapter mBTA = BluetoothAdapter.getDefaultAdapter();
    if (mBTA.isDiscovering()) mBTA.cancelDiscovery();
    mBTA.startDiscovery();
    ...
    
    /** In a broadcast receiver: */
    
    if (BluetoothDevice.ACTION_FOUND.equals(action)) { // One device found.
    
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        Log.d(TAG, "Start Pairing... with: " + device.getName());
        device.createBond();
    }
    
    // If you want to auto-input the pin#:
    else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
    
                        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                        device.setPin("1234".getBytes());
    }
    

提交回复
热议问题