現在、Android デバイス (4.4.2 KitKat) をプログラムで .NET プロジェクトの Bluetooth ドングルにペアリングしようとしています。
ユーザー入力 (パス キー ダイアログ) との通常のペアリングは機能しますが、ユーザー入力をバイパスしたいと考えています。
いくつかの再検索(here および here)の後、なんとかコードを思いつきましたが、ダイアログは引き続き表示されます。
以下に私の許可があります:
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH_PRIVILEGED" />
最小 API レベルは 19 に設定されているため、Bluetooth の特権アクセス許可が必要です。
特定のデバイスをペアリングするためのコード:
final String SERVER_MAC = "00:1A:7D:DA:71:07";
final BluetoothAdapter ba = BluetoothAdapter.getDefaultAdapter();
final ArrayList<BluetoothDevice> server = new ArrayList<>();
ba.startDiscovery();
BroadcastReceiver btReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(BluetoothDevice.ACTION_FOUND.equals(action)){
BluetoothDevice bd = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
Log.d(TAG,"Found device: " + bd.getName() + " : " + bd.getAddress());
if(bd.getAddress().equals(SERVER_MAC)){
Log.d(TAG,"Server bluetooth found");
server.add(bd);
Log.d(TAG,"creating bond");
bd.createBond();
ba.cancelDiscovery();
}
}
else if(BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){
Log.d(TAG,"setting info");
setBluetoothPairingPin(server.get(0));
}
}
};
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(btReceiver, filter);
IntentFilter pairingRequest = new IntentFilter(BluetoothDevice.ACTION_PAIRING_REQUEST);
registerReceiver(btReceiver,pairingRequest);
ACTION_PAIRING_REQUEST メソッド:
byte[] pairingPin = "123456".getBytes();
try{
Log.d(TAG,"Trying to set pin");
bluetoothDevice.getClass().getMethod("setPin", byte[].class).invoke(bluetoothDevice,pairingPin);
Log.d(TAG,"Success setting pin");
try{
bluetoothDevice.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(bluetoothDevice, true);
Log.d(TAG,"Success setting pairing confirmtion");
}
catch(Exception e){
Log.d(TAG,"Exception: " + e.getMessage());
}
}
catch(Exception e){
Log.d(TAG,"Exception 2: " + e.getMessage());
}
どんな情報でも役に立ちます。ありがとう!