Android配对蓝牙设备没有针脚对话

时间:2014-05-10 19:33:41

标签: android bluetooth

我希望将我的Android手机和SPP蓝牙配对而无需针脚和确认对话。我有这段代码,注册BroadcastReceiver

IntentFilter filter = new IntentFilter(ACTION_PAIRING_REQUEST);
                registerReceiver(mPairReceiver, filter); 
                pairDevice(device);

pairDevice 方法:

 private void pairDevice(BluetoothDevice device) {
    try {

        Method method = device.getClass().getMethod("createBond", (Class[]) null);
        method.invoke(device, (Object[]) null);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

广播接收器

private final BroadcastReceiver mPairReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_PAIRING_REQUEST.equals(action)) {    
            System.out.println("ACTION_PAIRING_REQUEST");
            setBluetoothPairingPin(device);
        } 
    }
};

setBluetoothPairingPin 方法:

public void setBluetoothPairingPin(BluetoothDevice device)
{
    byte[] pinBytes = convertPinToBytes("0000");
    try {
        Log.d(TAG, "Try to set the PIN");
        Method m = device.getClass().getMethod("setPin", byte[].class);
        m.invoke(device, pinBytes);
        Log.d(TAG, "Success to add the PIN.");
        try {
            device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, false);
            Log.d(TAG, "Success to setPairingConfirmation.");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.e(TAG, e.getMessage());
            e.printStackTrace();
        } 
    } catch (Exception e) {
        Log.e(TAG, e.getMessage());
        e.printStackTrace();
    }
}

但始终显示带引脚和确认的对话框。

0 个答案:

没有答案