GATT の前に、createRfcommSocketToServiceRecord、createInsecureRfcommSocketToServiceRecord
メソッドはペアリングされたデバイスを作ることができます、
ただし、GATT にはペアリングされたデバイスに関するオプションはなく、BluetoothDevice.connectGatt(...) のみを使用してください。
すでに接続されている場合はペアリングしたい。
どうも。
GATT の前に、createRfcommSocketToServiceRecord、createInsecureRfcommSocketToServiceRecord
メソッドはペアリングされたデバイスを作ることができます、
ただし、GATT にはペアリングされたデバイスに関するオプションはなく、BluetoothDevice.connectGatt(...) のみを使用してください。
すでに接続されている場合はペアリングしたい。
どうも。
私の知る限り、BLE でペアリング手順を開始するには、次の 2 つの方法があります。
1) API 19 以降では、 を呼び出してペアリングを開始できますmBluetoothDevice.createBond()
。ペアリング プロセスを開始するために、リモート BLE デバイスに接続する必要はありません。
2) ガット操作をしようとする場合、メソッドを例にとってみましょう
mBluetoothGatt.readCharacteristic(characteristic)
通信を行うためにリモート BLE デバイスを結合する必要がある場合、コールバック
onCharacteristicRead(
BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status)
が呼び出され、そのstatus
パラメータ値はGATT_INSUFFICIENT_AUTHENTICATION
またはと等しくGATT_INSUFFICIENT_ENCRYPTION
なり、 とは等しくなりませんGATT_SUCCESS
。これが発生すると、ペアリング手順が自動的に開始されます。
onCharacteristicRead
コールバックが呼び出されたときに失敗した場合を調べる例を次に示します。
@Override
public void onCharacteristicRead(
BluetoothGatt gatt,
BluetoothGattCharacteristic characteristic,
int status)
{
if(BluetoothGatt.GATT_SUCCESS == status)
{
// characteristic was read successful
}
else if(BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION == status ||
BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION == status)
{
/*
* failed to complete the operation because of encryption issues,
* this means we need to bond with the device
*/
/*
* registering Bluetooth BroadcastReceiver to be notified
* for any bonding messages
*/
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
mActivity.registerReceiver(mReceiver, filter);
}
else
{
// operation failed for some other reason
}
}
この操作によりペアリング手順が自動的に開始されると言及している他の人: Android Bluetooth Low Energy ペアリング
そして、これが受信機を実装する方法です
private final BroadcastReceiver mReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
final String action = intent.getAction();
if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
{
final int state = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothDevice.ERROR);
switch(state){
case BluetoothDevice.BOND_BONDING:
// Bonding...
break;
case BluetoothDevice.BOND_BONDED:
// Bonded...
mActivity.unregisterReceiver(mReceiver);
break;
case BluetoothDevice.BOND_NONE:
// Not bonded...
break;
}
}
}
};