Java中的蓝牙背景

时间:2019-03-10 17:08:11

标签: java android bluetooth

我正在尝试在后台不丢失蓝牙连接,这是我使用的代码的一部分。我在执行onDestroy方法时启动了服务

我什至不打印吐司面包。

enter code here
**** MainActivity class****
@Override
protected void onDestroy(){
    Intent i = new Intent(this, ServiceBluetooth.class);
    i.putExtra("bluetooth_device", address_device);
    startService(i);
    super.onDestroy();
}

这是我的服务代码

***** ServiceBluetooth service *****
public class ServiceBluetooth extends Service {

private BluetoothAdapter mBluetoothAdapter;
public static final String B_DEVICE = "MY DEVICE";
public static final String B_UUID = "00001101-0000-1000-8000-00805f9b34fb";
// 00000000-0000-1000-8000-00805f9b34fb

public static final int STATE_NONE = 0;
public static final int STATE_LISTEN = 1;
public static final int STATE_CONNECTING = 2;
public static final int STATE_CONNECTED = 3;

private ConnectBtThread mConnectThread;
private static ConnectedBtThread mConnectedThread;

private static Handler mHandler = null;
public static int mState = STATE_NONE;
public static String deviceName;
public static BluetoothDevice sDevice = null;
public Vector<Byte> packData = new Vector<>(2048);

//IBinder mIBinder = new LocalBinder();


@Nullable
@Override
public IBinder onBind(Intent intent) {
    //mHandler = getApplication().getHandler();
    return mBinder;
}

public void toast(String mess) {
    Toast.makeText(this, mess, Toast.LENGTH_SHORT).show();
}

private final IBinder mBinder = new LocalBinder();

public class LocalBinder extends Binder {
    ServiceBluetooth getService() {
        // Return this instance of LocalService so clients can call public 
methods
        return ServiceBluetooth.this;
    }
}

启动服务后即获得Mac地址。但是没有显示吐司

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    String deviceg = intent.getStringExtra("bluetooth_device");

    Toast.makeText(ServiceBluetooth.this, ""+deviceg, 
    Toast.LENGTH_SHORT).show();
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

    connectToDevice(deviceg);

    return START_STICKY;
}

已建立连接

private synchronized void connectToDevice(String macAddress) {
    BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(macAddress);
    if (mState == STATE_CONNECTING) {
        if (mConnectThread != null) {
            mConnectThread.cancel();
            mConnectThread = null;
        }
    }
    if (mConnectedThread != null) {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }
    mConnectThread = new ConnectBtThread(device);
    toast("connecting");
    mConnectThread.start();
    setState(STATE_CONNECTING);
}

private void setState(int state) {
    mState = state;
    if (mHandler != null) {
        // mHandler.obtainMessage();
    }
}

public synchronized void stop() {
    setState(STATE_NONE);
    if (mConnectThread != null) {
        mConnectThread.cancel();
        mConnectThread = null;
    }
    if (mConnectedThread != null) {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }
    if (mBluetoothAdapter != null) {
        mBluetoothAdapter.cancelDiscovery();
    }

    stopSelf();
}

数据交换方法

public void sendData(String message) {
    if (mConnectedThread != null) {
        mConnectedThread.write(message.getBytes());
        toast("sent data");
    } else {
        Toast.makeText(ServiceBluetooth.this, "Failed to send data", 
Toast.LENGTH_SHORT).show();
    }
}

@Override
public boolean stopService(Intent name) {
    setState(STATE_NONE);

    if (mConnectThread != null) {
        mConnectThread.cancel();
        mConnectThread = null;
    }

    if (mConnectedThread != null) {
        mConnectedThread.cancel();
        mConnectedThread = null;
    }

    mBluetoothAdapter.cancelDiscovery();
    return super.stopService(name);
}



private class ConnectBtThread extends Thread {
    private final BluetoothSocket mSocket;
    private final BluetoothDevice mDevice;

    public ConnectBtThread(BluetoothDevice device) {
        mDevice = device;
        BluetoothSocket socket = null;
        try {
            socket = 
 device.createInsecureRfcommSocketToServiceRecord(UUID.fromString(B_UUID));
        } catch (IOException e) {
            e.printStackTrace();
        }
        mSocket = socket;

    }

    @Override
    public void run() {
        mBluetoothAdapter.cancelDiscovery();

        try {
            mSocket.connect();
            Log.d("service", "connect thread run method (connected)");
            SharedPreferences pre = getSharedPreferences("BT_NAME", 0);
            pre.edit().putString("bluetooth_connected", 
mDevice.getName()).apply();

        } catch (IOException e) {

            try {
                mSocket.close();
                Log.d("service", "connect thread run method ( close 
function)");
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
        //connected(mSocket);
        mConnectedThread = new ConnectedBtThread(mSocket);
        mConnectedThread.start();
    }

    public void cancel() {

        try {
            mSocket.close();
            Log.d("service", "connect thread cancel method");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

private class ConnectedBtThread extends Thread {
    private final BluetoothSocket cSocket;
    private final InputStream inS;
    private final OutputStream outS;

    private byte[] buffer;

    public ConnectedBtThread(BluetoothSocket socket) {
        cSocket = socket;
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        try {
            tmpIn = socket.getInputStream();

        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }

        inS = tmpIn;
        outS = tmpOut;
    }

    @Override
    public void run() {
        buffer = new byte[1024];
        int mByte;
        try {
            mByte = inS.read(buffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.d("service", "connected thread run method");

    }


    public void write(byte[] buff) {
        try {
            outS.write(buff);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void cancel() {
        try {
            cSocket.close();
            Log.d("service", "connected thread cancel method");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@Override
public void onDestroy() {
    this.stop();
    super.onDestroy();
}
}

我在做什么错了?

0 个答案:

没有答案