【问题标题】:Bluetooth Data Transfer蓝牙数据传输
【发布时间】: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 但如果我使用它,我将无法发送消息,因为我收到此警告:片段:应用程序可能在其主线程上做了太多工作。所以我无法发送消息,因为我的主线程很忙。

标签: java android bluetooth


【解决方案1】:

我会用你所连接的设备初始化一个 BluetoothConnection,然后以这种方式发送数据。它看起来有点像这样:

BluetoothConnection connection = new BluetoothConnection(connectedDevice);

public void sendData(){

    String s = editText.getText().toString();

    byte[] b = s.getBytes();

    connection.write(b);

    //System.out.println("Bytes Sent");
}// end sendData

【讨论】:

  • 它不适用于我的项目。我有一个空对象错误这种类型的代码。因为我的蓝牙连接服务。 java 显示此警告:应用程序可能在其主线程上做太多工作。所以我无法发送消息,因为我的主线程很忙。所以如果我调试我的 ConnectedThread 为空。这是我的连接代码:paste.ubuntu.com/p/gcPrydZnDw
  • 这是我这个错误的老话题所以我尝试服务但我找不到任何解决方案stackoverflow.com/questions/51416401/…
  • 如果您在后台线程中创建连接,则可以将该连接的设备传递给主线程,然后在那里启动连接。您不应该在主线程上发生实际连接。
  • 我该怎么做? 5天我什么都做不了。如果您对此帖子或此stackoverflow.com/questions/51416401/… 有答案或建议,请提供帮助
  • 在您的另一篇文章中,我向您发送了 BluetoothConnection 类,在您实现此代码后将起作用。我的错误也没有包括在这篇文章中。
【解决方案2】:

您是否致电startService(sendData); 以启动蓝牙发送服务?您似乎只是在创建意图并用数据填充它,而不是启动它。

【讨论】:

  • 我有另一个 java 类来连接蓝牙。我收到关于这个 java 类的数据。 paste.ubuntu.com/p/gcPrydZnDw 我的主线程有问题,所以我创建了 DataSend 意图服务。当我按下发送按钮时,我的连接成功了。但我无法连接到这个 senData。那我该怎么办呢?
  • 我使用这个类paste.ubuntu.com/p/gcPrydZnDw和DataSend服务类
猜你喜欢
  • 2016-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多