2013-06-18 242 views
30

私はBluetoothデバイスに接続するアプリケーションを開発していますが、私はユーザーが必要なピンを入力しないようにしています。任意の接続に関連する問題があります...アプリケーションのみでピン認証プロセスを挿入して完了します。Bluetoothデバイスをプログラムでペアリングする方法

私はそれが動作していると確信していますが、このコードにピンを追加する方法は不明です。

private void pairDevice(BluetoothDevice device) { 
     try { 
      Log.d("pairDevice()", "Start Pairing..."); 
      Method m = device.getClass().getMethod("createBond", (Class[]) null); 
      m.invoke(device, (Object[]) null); 
      Log.d("pairDevice()", "Pairing finished."); 
     } catch (Exception e) { 
      Log.e("pairDevice()", e.getMessage()); 
     } 
    } 

誰もが問題を解決するために、上記のコードまたは任意の同様のコードでピンを入力する方法を知っています。.. これはあなたに答えを与えるように思わ

+0

おそらくこれが役に立ちます。 http://stackoverflow.com/questions/5885438/bluetooth-pairing-without-user-confirmation 乾杯、 –

+0

@ManolescuSebastian - 私は...安全な接続を作成したい –

+0

私の答えをお試しください。私はそれがあなたのために働くことを願って –

答えて

3

は、このコードを試してみてください。

public void pairDevice(BluetoothDevice device) 
{ 
    String ACTION_PAIRING_REQUEST = "android.bluetooth.device.action.PAIRING_REQUEST"; 
    Intent intent = new Intent(ACTION_PAIRING_REQUEST); 
    String EXTRA_DEVICE = "android.bluetooth.device.extra.DEVICE"; 
    intent.putExtra(EXTRA_DEVICE, device); 
    String EXTRA_PAIRING_VARIANT = "android.bluetooth.device.extra.PAIRING_VARIANT"; 
    int PAIRING_VARIANT_PIN = 0; 
    intent.putExtra(EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
    context.startActivity(intent); 
} 

Intent intent = new Intent(BluetoothDevice.ACTION_PAIRING_REQUEST); 
intent.putExtra(EXTRA_DEVICE, device); 
int PAIRING_VARIANT_PIN = 272; 
intent.putExtra(BluetoothDevice.EXTRA_PAIRING_VARIANT, PAIRING_VARIANT_PIN); 
sendBroadcast(intent); 

Intent intent = new Intent(Settings.ACTION_BLUETOOTH_SETTINGS); 
startActivityForResult(intent, REQUEST_PAIR_DEVICE); 

私はこれが

参考に役立ちます願っています:誰かが必要な場合はhttp://pastebin.com/N8dR4Aa1

+0

私を助けてください... BTが有効になっているデバイスのリストが必要です。特定のBluetoothデバイスをクリックした後、デバイスとペアになります –

+2

私は最初の方法を取得しますが、他の2ブロックのコードで何を提案しますか? – gregm

1

だから

BluetoothDevice device = intent.getParcelableExtra("android.bluetooth.device.extra.DEVICE"); 
device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true); 
device.getClass().getMethod("cancelPairingUserInput", boolean.class).invoke(device); 
8

、これを試してみてくださいを、私は、この質問を持っていましたこれに対する答えはアンドロイド4.4.2で働いています。

IntentFilter filter = new IntentFilter(
       "android.bluetooth.device.action.PAIRING_REQUEST"); 


     /* 
     * Registering a new BTBroadcast receiver from the Main Activity context 
     * with pairing request event 
     */ 
     registerReceiver(
       new PairingRequest(), filter); 

レシーバのコード。

public static class PairingRequest extends BroadcastReceiver { 
     public PairingRequest() { 
      super(); 
     } 

     @Override 
     public void onReceive(Context context, Intent intent) { 
      if (intent.getAction().equals("android.bluetooth.device.action.PAIRING_REQUEST")) { 
       try { 
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
        int pin=intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY", 0); 
        //the pin in case you need to accept for an specific pin 
        Log.d("PIN", " " + intent.getIntExtra("android.bluetooth.device.extra.PAIRING_KEY",0)); 
        //maybe you look for a name or address 
        Log.d("Bonded", device.getName()); 
        byte[] pinBytes; 
        pinBytes = (""+pin).getBytes("UTF-8"); 
        device.setPin(pinBytes); 
        //setPairing confirmation if neeeded 
        device.setPairingConfirmation(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 

マニフェストファイルにあります。

<uses-permission android:name="android.permission.BLUETOOTH" /> 
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> 

そして、broadcastReceiver。 PINコードを設定する方法

<receiver android:name=".MainActivity$PairingRequest"> 
       <intent-filter> 
        <action android:name="android.bluetooth.device.action.PAIRING_REQUEST" /> 
        <action android:name="android.bluetooth.device.action.PAIRING_CANCEL" /> 
       </intent-filter> 
</receiver> 
1
BluetoothSocket bluetoothSocket = null; 
    try { 
     bluetoothSocket = device.createRfcommSocketToServiceRecord(UUID.fromString(UUID_DIVING)); 
    } catch (IOException e) { 
     Log.i("Bluetooth", "IOException = " + e.getMessage()); 
     e.printStackTrace(); 
    } 

    try { 
     byte[] pin = (byte[]) BluetoothDevice.class.getMethod("convertPinToBytes", String.class).invoke(BluetoothDevice.class, "0000"); 
     Method m = device.getClass().getMethod("setPin", byte[].class); 
     m.invoke(device, (Object) pin); 
     device.getClass().getMethod("setPairingConfirmation", boolean.class).invoke(device, true); 
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { 
     Log.i("Bluetooth", "IOException = " + e.getMessage()); 
     e.printStackTrace(); 
    } 

    try { 
     if (bluetoothSocket != null) { 
      bluetoothSocket.connect(); 
      Log.i("Bluetooth", "bluetoothSocket.connect() "); 
      InputStream inputStream = bluetoothSocket.getInputStream(); 
      OutputStream outputStream = bluetoothSocket.getOutputStream(); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
0

上記の答え(と、それは私を助けて)されています。しかし、私はAndroid 6で動作する以下の簡単なコードを共有しています:

BluetoothAdapter mBTA = BluetoothAdapter.getDefaultAdapter(); 
if (mBTA.isDiscovering()) mBTA.cancelDiscovery(); 
mBTA.startDiscovery(); 
... 

/** In a broadcast receiver: */ 

if (BluetoothDevice.ACTION_FOUND.equals(action)) { // One device found. 

    BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
    Log.d(TAG, "Start Pairing... with: " + device.getName()); 
    device.createBond(); 
} 

// If you want to auto-input the pin#: 
else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)){ 

        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); 
        device.setPin("1234".getBytes()); 
} 
関連する問題