我正在開發一個Android應用程序,可以將數據傳輸到4.0藍牙串行設備。我由LeGatt android示例項目(http://developer.android.com/samples/BluetoothLeGatt/index.html)指導。在這個項目中,他們連接到設備,但沒有關於傳輸數據。爲4.0藍牙傳輸創建一個插座
2.0藍牙,我可以創建一個Socket,InputStream和OutputStream來傳輸數據,這樣的事情:
protected BluetoothSocket mySocket = null;
private InputStream MyInStream;
private OutputStream MyOutStream;
try {
Method m = mBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(mBluetoothDevice, Integer.valueOf(1));
} catch (Exception e) {
textViewLog.append("\n"+"CONNECTION IN THREAD DIDNT WORK");
}
mySocket = tmp;
try {
mySocket.connect();
} catch (IOException e) {
textViewLog.append("\n"+e.getMessage());
textViewLog.append("\n"+"CONNECTION IN THREAD DIDNT WORK 2");
}
try {
MyInStream = mySocket.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
try {
MyOutStream = mySocket.getOutputStream();
} catch (IOException e) {
textViewLog.append("\nERROR: "+e.getMessage());
}
try {
MyOutStream.write((letra+"\r").getBytes());
} catch (IOException e) {
textViewLog.append("\nERROR: "+e.getMessage());
}
但在4.0藍牙我無法創建插槽,因爲此方法不作品
try {
Method m = mBluetoothDevice.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
tmp = (BluetoothSocket) m.invoke(mBluetoothDevice, Integer.valueOf(1));
} catch (Exception e) {
textViewLog.append("\n"+"CONNECTION IN THREAD DIDNT WORK");
}
有人可以幫助我使用我的4.0藍牙設備達到數據傳輸。
您不需要爲BLE使用輸入/輸出流!它工作不同! – Tim