如何在android中通过蓝牙向配对设备发送短信?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21476835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to send a text message to a paired device through bluetooth in android?
提问by Ekanta Swain
In my app I want to send and receive text message through bluetooth. I can see in my listview a list of paired device name and address.But when I am trying to send a text to a paired device nothing happens. In other device there is no text received.
在我的应用程序中,我想通过蓝牙发送和接收短信。我可以在我的列表视图中看到配对设备名称和地址的列表。但是当我尝试向配对设备发送文本时,没有任何反应。在其他设备中没有收到文本。
This is my Code to send message to a paired device.
这是我向配对设备发送消息的代码。
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);
}
}
回答by user2469133
the bluetoothchat sample is actually the perfect thing to use if you are new in using the bluetooth api.
如果您不熟悉使用蓝牙 api,则蓝牙聊天示例实际上是完美的选择。
assuming that you are using only one Activity for your application which is the BluetoothChat class :
假设您的应用程序只使用一个 Activity,即 BluetoothChat 类:
for sending text to the device you are connected to, use the "sendMessage(String message)" method in the BluetoothChat class to send text.
要向您连接的设备发送文本,请使用 BluetoothChat 类中的“sendMessage(String message)”方法发送文本。
as for receiving and handling the text, you will find also handleMessage(Message msg) method somewhere in the bluetoothchat class then go this part:
至于接收和处理文本,您还会在 bluetoothchat 类中的某处找到 handleMessage(Message msg) 方法,然后转到这一部分:
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
// construct a string from the valid bytes in the buffer
String readMessage = new String(readBuf, 0, msg.arg1);
see the readMessage String?
看到 readMessage 字符串了吗?
this is the text that that you receive from the other device , now you can handle it as you want.
这是您从其他设备收到的文本,现在您可以根据需要处理它。
then simply change the main layout that the BluetoothChat class refers to, then in BluetoothChat chat either comment or delete the parts that have errors which actually will be the parts in the UI u have deleted or changed.
然后只需更改BluetoothChat 类所引用的主要布局,然后在BluetoothChat 聊天中评论或删除有错误的部分,这些部分实际上将是您已删除或更改的UI 部分。
i know the code may sound messy but this is the easiest way to use it quickly as possible and watching video tutorials or text tutorials for hours will just make it more complicated, believe me i tried this before.
我知道代码可能听起来很乱,但这是尽可能快速使用它的最简单方法,并且观看视频教程或文本教程几个小时只会让它变得更加复杂,相信我我以前试过这个。