1
votes

I want to implement passkey for connect device or establish the connection with BLE device. I have 6 byte passkey, So how we will create bond or pairing and send passkey to the BLE device using RxAndroid library.

Can any one give me reference or document link or demo link for implementation of Passkey or create bond or pairing while establish connection.

Thanks in advance!!

1
Hi if i'll share the code in java will that do.Udit Kapahi
@Udit Kapahi , Please Can you provide link ?Nitin Karande
I dont have the link I had to write it as an answer... So could you please tell me what exactly do u need hereUdit Kapahi
Where did ur research ended up till now?Udit Kapahi
I am trying to create bond while establish connection. Can you give me idea of function or observer or class name I will do thatNitin Karande

1 Answers

3
votes

First you need to find the nearby devices.

public void registerDeviceForAnyNEwDeviceFound() {
    // Register for broadcasts when a device is discovered.
    mBluetoothAdapter.startDiscovery();
    IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
    registerReceiver(mSearchReceiver, filter);
    Log.e("STATUS", "" +
            mBluetoothAdapter.isDiscovering());
    isSearchReceiverRegistered = true;
}

// Create a BroadcastReceiver for ACTION_FOUND.
private final BroadcastReceiver mSearchReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.e("SEARCH STARTED", "TRUE");
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            // Discovery has found a device. Get the BluetoothDevice
            // object and its info from the Intent.
            try {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                String deviceName = device.getName();
                String deviceHardwareAddress = device.getAddress(); // MAC address
                Device mDevice = new Device();
                mDevice.setDeviceName(deviceName);
                mDevice.setDeviceAddress(deviceHardwareAddress);
                mDevice.setmBluetoothDevice(device);

                Log.e("Discovered DEVICE", deviceName);
                Log.e("BOND STATE", "" + device.getBondState());

                nearbyDeviceList.add(mDevice);
                mNearbyDeviceRecAdapter.notifyDataSetChanged();

            } catch (Exception e) {
                Log.e("EXCEPTION", e.getLocalizedMessage());
            }

        }
    }
};

Then you need to register another BroadCastReceiver, this receiver will listen to the bond changes with the device with whom you are going to pair with.

IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
            registerReceiver(mPairingReceiver, filter);

 private final BroadcastReceiver mPairingReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)) {
            BluetoothDevice mDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            //3 cases:
            //case1: bonded already
            if (mDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
                Log.d(TAG, "BroadcastReceiver: BOND_BONDED.");
                Log.e(TAG, "BroadcastReceiver: BOND_BONDED." + mDevice.getName());
                mBluetoothAdapter.cancelDiscovery();

            }
            //case2: creating a bone
            if (mDevice.getBondState() == BluetoothDevice.BOND_BONDING) {
                Log.d(TAG, "BroadcastReceiver: BOND_BONDING.");
                Log.e(TAG, "BroadcastReceiver: BOND_BONDING." + mDevice.getName());
            }
            //case3: breaking a bond
            if (mDevice.getBondState() == BluetoothDevice.BOND_NONE) {
                Log.d(TAG, "BroadcastReceiver: BOND_NONE.");
                Log.e(TAG, "BroadcastReceiver: BOND_NONE." + mDevice.getName());
            }
        }
    }
};

Finally when you want to pair with the device call:

mNearbyDeviceRecAdapter.getItemAtPosition(position).getmBluetoothDevice().createBond();

Once you call this statement, this will initiate pairing with the device.

Hope! this helps.