Java 蓝牙数据传输

Java 蓝牙数据传输,java,android,bluetooth,Java,Android,Bluetooth,我正在尝试使用Bluetoothintent服务使用这些代码发送数据,但无法向设备发送消息。我正在调试此代码,但DataSend类元素似乎为空。如何解决此问题,或者如何使用intent service正确编写发送消息代码 我这样称呼这个片段: 最终意图sendData=新意图(getActivity(),DataSend.class) 这是我的数据端服务代码: public class DataSend extends IntentService{ private static fin

我正在尝试使用
Bluetooth
intent服务使用这些代码发送数据,但无法向设备发送消息。我正在调试此代码,但DataSend类元素似乎为空。如何解决此问题,或者如何使用intent service正确编写发送消息代码

我这样称呼这个片段:

最终意图sendData=新意图(getActivity(),DataSend.class)

这是我的数据端服务代码:

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);
        }
    }

}

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

我会初始化与所连接设备的蓝牙连接,然后以这种方式发送数据。看起来有点像这样:

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

什么是
null
以及什么阻止了您这么做?我把断点放在这行:sendData.putExtra(“test”,s);在我的碎片。我看到所有DataSend元素都为空。所以我看不到任何日志、返回值或其他内容。我不明白为什么你在任何地方都有一个“BluetoothConnection”,如果你有,那么这是一个简单的“.write”命令。它在这里,但如果我使用它,我就无法发送消息,因为我收到了以下警告:片段:应用程序可能在其主线程上做了太多的工作。所以我不能发送消息,因为我的主线程忙。我有另一个java类来连接蓝牙。我接收这个java类的数据。我的主线程有问题,所以我创建了DataSend意图服务。当我按下发送按钮时,我的连接成功了。但我无法与此senData连接。那么我该怎么做呢?我使用这个类和DataSend服务classIt的意思是,它不在我的项目中工作。我在这类代码中有一个空对象错误。因为我的蓝牙连接服务。java显示了这个警告:应用程序可能在其主线程上做了太多的工作。所以我不能发送消息,因为我的主线程忙。因此,如果我调试我的ConnectedThread null。这是我的连接代码:这是我关于这个错误的老话题,所以我尝试提供服务,但我找不到任何解决方案。如果您在后台线程中创建连接,那么您可以将连接的设备传递到主线程,然后在那里启动连接。你不应该在你的主线程上有实际的连接。我怎么做?5天内我什么都做不了。如果你对这篇文章或这篇文章有答案或建议,请在你的另一篇文章上提供帮助,我给你发送了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