【发布时间】:2018-07-26 21:17:23
【问题描述】:
我正在尝试使用这些代码使用 Bluetooth 意图服务发送数据,但我无法向设备发送消息。我正在调试此代码,但 DataSend 类元素似乎为空。我该如何解决这个问题,或者如何使用 Intent 服务编写正确的发送消息代码?
我是这样调用片段的:
最终意图 sendData = new Intent(getActivity(),DataSend.class);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = editText.getText().toString();
sendData.putExtra("test",s);
}
});
这是我的 DataSend 服务代码:
public class DataSend extends IntentService{
private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
private static final String TAG = "DataTransmissionService";
private BluetoothAdapter btAdapter = null;
private BluetoothSocket btSocket = null;
private OutputStream outStream = null;
private BluetoothDevice device = null;
private Log log;
public DataSend() {
super("DataSend");
}
@Override
protected void onHandleIntent(Intent intent) {
cleanup();
if (intent != null){
btAdapter = BluetoothAdapter.getDefaultAdapter();
String pairedDeviceAddress = "38:1B:4B:98:E7:ED";
try {
log.d(TAG, pairedDeviceAddress);
device = btAdapter.getRemoteDevice(pairedDeviceAddress);
log.d(TAG, "Device bond state : " + device.getBondState());
} catch (Exception e) {
log.e(TAG, "Invalid address: " + e.getMessage());
return;
}
try {
btSocket = device.createRfcommSocketToServiceRecord(MY_UUID);
} catch (IOException e) {
log.e(TAG, "Socket creation failed: " + e.getMessage());
return;
}
try {
if (!btSocket.isConnected()) {
btSocket.connect();
log.d(TAG, "Connected");
} else {
log.d(TAG, "Already Connected"); //flow never reaches here for any use case
}
} catch (IOException e) {
log.e(TAG, "btSocket.connect() failed : " + e.getMessage());
return;
}
try {
outStream = btSocket.getOutputStream();
} catch (IOException e) {
log.e(TAG, "Failed to get output stream:" + e.getMessage());
return;
}
sendData("test");
//cleanup(); called in onDestroy()
}
}
private void cleanup(){
try {
if (outStream != null) {
outStream.close();
outStream = null;
}
} catch (Exception e) {
log.e(TAG, "Failed to close output stream : " + e.getMessage());
}
try {
if (btSocket != null) {
btSocket.close();
btSocket = null;
}
}catch (Exception e) {
log.e(TAG, "Failed to close connection : " + e.getMessage());
}
}
private BluetoothSocket createBluetoothSocket(BluetoothDevice device) throws IOException {
if(Build.VERSION.SDK_INT >= 10){
try {
final Method m = device.getClass().getMethod("createInsecureRfcommSocketToServiceRecord", new Class[] { UUID.class });
return (BluetoothSocket) m.invoke(device, MY_UUID);
} catch (Exception e) {
log.e(TAG, "Could not create Insecure RFComm Connection",e);
}
}
return device.createRfcommSocketToServiceRecord(MY_UUID);
}
private void sendData(String message) {
byte[] msgBuffer = message.getBytes();
log.d(TAG, "Sending : " + message);
try {
outStream.write(msgBuffer);
} catch (IOException e) {
log.e(TAG, "failed to write " + message);
}
}
}
【问题讨论】:
-
null到底是什么,它阻止了你做什么? -
我把断点放在这一行:sendData.putExtra("test",s);在我的片段。我看到所有 DataSend 元素都为空。所以我看不到任何日志、返回值或其他任何东西。我不明白为什么
-
你在任何地方都有'BluetoothConnection'吗,如果你有,那么它就是一个简单的'.write'命令。
-
它在这里paste.ubuntu.com/p/gcPrydZnDw 但如果我使用它,我将无法发送消息,因为我收到此警告:片段:应用程序可能在其主线程上做了太多工作。所以我无法发送消息,因为我的主线程很忙。