2010-10-01 63 views
1

已經配對的設備我正在開發Android應用程序連接到支持藍牙串行端口配置文件的簡單設備(SPP)。我能夠成功連接和交換數據,但每次連接時都會提示用戶輸入設備的PIN碼。連接時提示輸入藍牙PIN碼,即可在Android

在藍牙設置我可以看到,該裝置「由不連接」配對。

的提示是一個問題,因爲如果用戶不輸入PIN足夠快,插座連接超時,用戶必須再試一次。下面的代碼

相關位...

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.scanlayout); 
    ... 
    _Context = this; 
    _ActivityCreated = true; 
    _ReceivedText = (TextView)findViewById(R.id._Scan_Results); 
    _BluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); 
    _BluetoothDevice = _BluetoothAdapter.getRemoteDevice(_DeviceAddress); 
    _BusySpinner = ProgressDialog.show(_Context, "", "Connecting to scanner..."); 
    new ConnectToScannerTask().execute(_BluetoothDevice); 
} 
private final Handler scanReceivedHandler = new Handler() 
{ 
@Override 
public void handleMessage(Message message) 
{ 
    String receivedText = (String)message.obj; 
    _ReceivedText.setText(receivedText); 
} 
}; 

private class ConnectToScannerTask extends AsyncTask<BluetoothDevice, Void, InputStream> 
{ 
@Override 
protected InputStream doInBackground(BluetoothDevice... params) 
{ 
    BluetoothDevice device = params[0]; 
    try 
    { 
    _Socket = device.createRfcommSocketToServiceRecord(WELL_KNOWN_UUID); 
    _BluetoothAdapter.cancelDiscovery(); 
    _Socket.connect(); 
    return _Socket.getInputStream(); 
    } 
    catch (IOException e) 
    { 
    Log.d("ScanActivity.ConnectToScannerTask.doInBackground", e.getMessage()); 
    } 
    return null; 
} 

@Override 
protected void onPostExecute(final InputStream result) 
{ 
    _BusySpinner.dismiss(); 

    if (result == null) 
    { 
    _ReceivedText.setText("Failed to connect to scanner."); 
    return; 
    } 

     Thread thread = new Thread() 
     { 
     @Override 
     public void run() 
     { 
     byte[] buffer = new byte[1024]; 
    try 
    { 
     while (_ActivityCreated) 
     { 
     Arrays.fill(buffer, (byte) 0); 
     int bytesRead = result.read(buffer); 
     if (bytesRead > 0) 
     { 
     Message message = scanReceivedHandler.obtainMessage(1, new String(buffer)); 
     message.sendToTarget(); 
     Log.e("ScanActivity", "Received: " + new String(buffer)); 
     } 
     if (bytesRead < 0) 
     { 
     break; 
     } 
     } 
     Message message = scanReceivedHandler.obtainMessage(1, "End of Stream"); 
     message.sendToTarget(); 
     Log.e("ScanActivity", "End of Stream"); 
    } 
    catch (Exception e) 
    { 
     Message message = scanReceivedHandler.obtainMessage(1, "Connection to scanner lost"); 
     message.sendToTarget(); 
     Log.e("ScanActivity", e.getMessage()); 
    } 
    try 
    { 
     _Socket.close(); 
    } 
    catch (IOException e) 
    { 
     Log.e("ScanActivity", e.getMessage()); 
    } 
     } 
     }; 
     thread.start(); 
    } 
} 

只要用戶快有關輸入PIN,連接成功,我可以接收數據。我的直覺是我錯過了一個設置步驟。雖然我對BT的具體細節並不熟悉,但我不確定這是否會成爲設備強制輸入PIN的問題?

回答

1

這可能是與不保持裝置接合,該遠程設備的一個問題(意味着在存儲連接鍵在隨後的連接被使用),這將導致在被輸入重新配對每個時間和需要PIN 。 一旦成功配對,Android通常應該存儲綁定信息。

+0

是否有任何變通,如果是這樣的話? – JadeMason 2011-03-21 19:58:20

+0

你可以嘗試,並檢查是否有已經粘合在列表中的設備 - 有時當列表已滿 – 2011-03-21 20:06:20

+0

不幸的是,沒有發生這種情況。該設備是條形碼掃描儀。它的診斷非常有限。它會告訴我它配置讀取哪些條形碼,但這是關於所有條形碼的。 – JadeMason 2011-03-22 20:06:18