Implement Bluetooth Connection into Service or Application Class without losing connection into a Device

后端 未结 2 1196
谎友^
谎友^ 2021-01-03 06:58

i need some help, can you explain to me how can i implement the Bluetooth Connection from my Application into my Mini Thermal Printer Device.

The scenario is like t

2条回答
  •  误落风尘
    2021-01-03 07:50

    Narayan's answer needs a corrections. I believe the onStartCommand function should read.

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("BT SERVICE", "SERVICE STARTED");
        bluetoothIn = new Handler() {
            public void handleMessage(android.os.Message msg) {
                Log.d("DEBUG", "handleMessage");
                if (msg.what == handlerState) { //if message is what we want
                    String readMessage = (String) msg.obj; // msg.arg1 = bytes from connect thread
                    recDataString.append(readMessage);//`enter code here`
                    Log.d("RECORDED", recDataString.toString());
                    // Do stuff here with your data, like adding it to the database
                }
                recDataString.delete(0, recDataString.length()); //clear all string data
            }
        };
        btAdapter = BluetoothAdapter.getDefaultAdapter(); // get Bluetooth adapter
        checkBTState();
        return super.onStartCommand(intent, flags, startId);
    }
    

提交回复
热议问题