Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void CheckSyncButtonPress(View view) {
- int REQUEST_ENABLE_BT=1;
- //sync button pressed
- //check that we can use bluetooth
- BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
- // Device supports bluetooth so let's try and enable it
- if (mBluetoothAdapter == null) {
- // Device does not support Bluetooth
- }
- else {
- if (!mBluetoothAdapter.isEnabled()) {
- //enable message popup
- Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
- startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
- }
- else
- {//if it IS enabled
- Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
- android.bluetooth.BluetoothDevice targetDevice = null; //variable for found device
- String targetName="HC-06";
- if (pairedDevices.size() > 0) {
- // If there are paired devices
- // Loop through paired devices
- for (BluetoothDevice device : pairedDevices) {
- if (Objects.equals(device.getName().trim(), targetName))
- {
- targetDevice=device;
- found=true;
- //found one that has the right name
- }
- }
- }
- if (found==true)
- {
- //we have a target device for connection
- UUID uuid = UUID.fromString("00001101-0000-1000-8000-00805f9b34fb"); //Standard SerialPortService ID
- try {
- android.bluetooth.BluetoothSocket mmSocket = targetDevice.createRfcommSocketToServiceRecord(uuid);
- mmSocket.connect();
- mmOutputStream = mmSocket.getOutputStream();
- mmInputStream = mmSocket.getInputStream();
- CheckBox checkBox = (CheckBox)findViewById(R.id.StatusBox);
- checkBox.setChecked(true);
- workerThread.start();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement