【发布时间】:2014-01-31 09:45:56
【问题描述】:
在我的应用程序中,我想通过蓝牙发送和接收短信。我可以在我的列表视图中看到配对设备名称和地址的列表。但是当我尝试向配对设备发送文本时没有任何反应。在其他设备中没有收到文本。
这是我向配对设备发送消息的代码。
private void sendDataToPairedDevice(String message, String adress) {
byte[] toSend = message.getBytes();
try {
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(adress);
// BluetoothSocket socket
// =device.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"));
BluetoothSocket socket = null;
Method m = null;
try {
m = device.getClass().getMethod("createRfcommSocket",
new Class[] { int.class });
} catch (Exception e) {
e.printStackTrace();
}
try {
socket = (BluetoothSocket) m.invoke(device, 1);
} catch (Exception e) {
e.printStackTrace();
}
OutputStream mmOutStream = socket.getOutputStream();
mBluetoothAdapter.cancelDiscovery();
socket.connect();
mmOutStream.write(toSend);
} catch (Exception e) {
Log.d("TAG", "Exception during write", e);
}
}
【问题讨论】:
-
短信只能通过短信网关发送,不能通过蓝牙发送?
-
@Ryan,OP 在哪里提到他要发送短信?
-
我想通过蓝牙发送它。哦,假设我想发送其他东西,但只能通过蓝牙发送。我成功获取了所有蓝牙设备名称和地址,现在我想发送一些东西到特定设备。
-
@user3243163 我的假设是
text message指的是短信,而不是包含短信以外的文本的文字消息。 -
@KanchaEkant 您是否从 BluetoothChat 示例应用程序中获得此代码?
标签: android bluetooth stream android-bluetooth