Bluetooth のペアリングを自動で実行しようと思い、BluetoothDevice の createBond() と、
setPin(byte) を呼ぼうとしたが、Android OSバージョン 2.1 で既には隠されたメソッドになっている。
しかたないので、リフレクションで実行を試してみる。
Method createBondMethod = BluetoothDevice.class.getMethod("createBond", new Class{});
createBondMethod.invoke(device);
Method convertPinToBytesMethod = BluetoothDevice.class.getMethod("convertPinToBytes", new Class{ String.class });
convertPinToBytesMethod.invoke(BluetoothDevice.class, "0000");
// PINコード登録
Method setPinMethod = device.getClass().getMethod("setPin", new Class{ byte[].class });
setPinMethod.invoke(device, pinCodes);
////////////////////////
を実行して、android.bluetooth.device.action.BOND_STATE_CHANGED の Broadcast 受信する
public class BondRecevier extends BroadcastReceiver{
@Override
public void onReceive(Context context,Intent intent){
if (action.equals(BluetoothDevice.ACTION_BOND_STATE_CHANGED)){
int prev_bond_state = intent.getExtras().getInt(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE);
int bond_state = intent.getExtras().getInt(BluetoothDevice.EXTRA_BOND_STATE);
if (prev_bond_state==BluetoothDevice.BOND_BONDING
&& bond_state==BluetoothDevice.BOND_BONDED) {
BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
// 接続の実行
}
と試してみたが、ペアリング入力のダイアログが表示されてしまう。