Example usage for android.bluetooth BluetoothGatt GATT_SUCCESS

List of usage examples for android.bluetooth BluetoothGatt GATT_SUCCESS

Introduction

In this page you can find the example usage for android.bluetooth BluetoothGatt GATT_SUCCESS.

Prototype

int GATT_SUCCESS

To view the source code for android.bluetooth BluetoothGatt GATT_SUCCESS.

Click Source Link

Document

A GATT operation completed successfully

Usage

From source file:Main.java

public static String getGattStatus(int status) {
    switch (status) {
    case BluetoothGatt.GATT_SUCCESS:
        return "GATT_SUCCESS";

    case BluetoothGatt.GATT_READ_NOT_PERMITTED:
        return "GATT_READ_NOT_PERMITTED";

    case BluetoothGatt.GATT_WRITE_NOT_PERMITTED:
        return "GATT_WRITE_NOT_PERMITTED";

    case BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION:
        return "GATT_INSUFFICIENT_AUTHENTICATION";

    case BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED:
        return "GATT_REQUEST_NOT_SUPPORTED";

    case BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION:
        return "GATT_INSUFFICIENT_ENCRYPTION";

    case BluetoothGatt.GATT_INVALID_OFFSET:
        return "GATT_INVALID_OFFSET";

    case BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH:
        return "GATT_INVALID_ATTRIBUTE_LENGTH";

    case BluetoothGatt.GATT_FAILURE:
        return "GATT_FAILURE";

    default:/*from  ww w  .ja  v a 2 s  . c o  m*/
        return "STATE_UNKNOWN: " + status;
    }
}

From source file:Main.java

public static String gattStatusToString(int status) {
    String str = "";
    switch (status) {
    case BluetoothGatt.GATT_SUCCESS:
        str = "GATT_SUCCESS";
        break;/*  w w w  . j a  va 2  s .  c  om*/
    case BluetoothGatt.GATT_READ_NOT_PERMITTED:
        str = "GATT_READ_NOT_PERMITTED";
        break;
    case BluetoothGatt.GATT_WRITE_NOT_PERMITTED:
        str = "GATT_WRITE_NOT_PERMITTED";
        break;
    case BluetoothGatt.GATT_INSUFFICIENT_AUTHENTICATION:
        str = "GATT_INSUFFICIENT_AUTHENTICATION";
        break;
    case BluetoothGatt.GATT_REQUEST_NOT_SUPPORTED:
        str = "GATT_REQUEST_NOT_SUPPORTED";
        break;
    case BluetoothGatt.GATT_INSUFFICIENT_ENCRYPTION:
        str = "GATT_INSUFFICIENT_ENCRYPTION";
        break;
    case BluetoothGatt.GATT_INVALID_OFFSET:
        str = "GATT_INVALID_OFFSET";
        break;
    case BluetoothGatt.GATT_INVALID_ATTRIBUTE_LENGTH:
        str = "GATT_INVALID_ATTRIBUTE_LENGTH";
        break;
    case BluetoothGatt.GATT_CONNECTION_CONGESTED:
        str = "GATT_CONNECTION_CONGESTED";
        break;
    case BluetoothGatt.GATT_FAILURE:
        str = "GATT_FAILURE";
        break;
    }

    return str;
}

From source file:fr.bmartel.android.bluetooth.connection.BluetoothDeviceConn.java

/**
 * Build Bluetooth device connection/*from www  . j ava2s  .co  m*/
 *
 * @param address
 */
@SuppressLint("NewApi")
public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) {
    this.deviceAddr = address;
    this.deviceName = deviceName;
    this.manager = manager;

    gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

            String intentAction;

            if (newState == BluetoothProfile.STATE_CONNECTED) {

                intentAction = ActionFilterGatt.ACTION_GATT_CONNECTED;
                Log.i(TAG, "Connected to GATT server.");
                Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

                connected = false;
                intentAction = ActionFilterGatt.ACTION_GATT_DISCONNECTED;
                Log.i(TAG, "Disconnected from GATT server.");
                manager.broadcastUpdate(ActionFilterGatt.ACTION_GATT_DISCONNECTED);

                if (BluetoothDeviceConn.this.gatt != null) {
                    BluetoothDeviceConn.this.gatt.close();
                }
            }
        }

        @Override
        // New services discovered
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {

                Runnable test = new Runnable() {
                    @Override
                    public void run() {

                        //you can improve this by using reflection
                        device = new DottiDevice(BluetoothDeviceConn.this);

                        device.addInitListener(new IDeviceInitListener() {
                            @Override
                            public void onInit() {
                                try {

                                    String name = "";

                                    JSONObject object = new JSONObject();
                                    object.put("address", getAddress());
                                    object.put("deviceName", getDeviceName());

                                    ArrayList<String> values = new ArrayList<String>();
                                    values.add(object.toString());

                                    connected = true;

                                    //when device is fully intitialized broadcast service discovery
                                    manager.broadcastUpdateStringList(
                                            ActionFilterGatt.ACTION_GATT_SERVICES_DISCOVERED, values);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        device.init();
                    }
                };
                Thread testThread = new Thread(test);
                testThread.start();

            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            System.out.println("characteristic write received ");
            manager.getEventManager().set();
            if (device != null) {
                device.notifyCharacteristicWriteReceived(characteristic);
            }
        }

        @Override
        // Result of a characteristic read operation
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            manager.getEventManager().set();
            if (device != null) {
                device.notifyCharacteristicReadReceived(characteristic);
            }
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            System.out.println("descriptor write received ");
            manager.getEventManager().set();
        }

        @Override
        // Characteristic notification
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            System.out.println("descriptor change received ");

            if (device != null) {
                device.notifyCharacteristicChangeReceived(characteristic);
            }
        }
    };
}

From source file:org.physical_web.physicalweb.BluetoothSite.java

@Override
public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
    // Make sure the site is running.  It can stop if the dialog is dismissed very quickly.
    if (!isRunning()) {
        close();// ww  w  . j  ava  2 s .  c  o m
        return;
    }

    // Make sure the read was successful.
    if (status != BluetoothGatt.GATT_SUCCESS) {
        Log.i(TAG, "onCharacteristicRead unsuccessful: " + status);
        close();
        Toast.makeText(activity, R.string.ble_download_error_message, Toast.LENGTH_SHORT).show();
        return;
    }

    // Record the data.
    Log.i(TAG, "onCharacteristicRead successful");
    try {
        mHtml.write(characteristic.getValue());
    } catch (IOException e) {
        Log.e(TAG, "Could not write to buffer", e);
        close();
        return;
    }

    // Request a new read if we are not done.
    if (characteristic.getValue().length == transferRate) {
        gatt.readCharacteristic(this.characteristic);
        return;
    }

    // At this point we are done.  Show the file.
    Log.i(TAG, "transfer is complete");
    close();
    openInChrome(getHtmlFile());
}

From source file:fr.bmartel.android.notti.service.bluetooth.connection.BluetoothDeviceConn.java

/**
 * Build Bluetooth device connection/* w  w  w  . ja va  2s  .  c  o  m*/
 *
 * @param address
 */
@SuppressLint("NewApi")
public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) {
    this.deviceAddr = address;
    this.deviceName = deviceName;
    this.manager = manager;

    gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

            if (newState == BluetoothProfile.STATE_CONNECTED) {

                Log.i(TAG, "Connected to GATT server.");
                Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

                connected = false;
                Log.i(TAG, "Disconnected from GATT server.");

                try {
                    JSONObject object = new JSONObject();
                    object.put(BluetoothConst.DEVICE_ADDRESS, getAddress());
                    object.put(BluetoothConst.DEVICE_NAME, getDeviceName());

                    ArrayList<String> values = new ArrayList<String>();
                    values.add(object.toString());

                    //when device is fully intitialized broadcast service discovery
                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCONNECTED, values);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (BluetoothDeviceConn.this.gatt != null) {
                    BluetoothDeviceConn.this.gatt.close();
                }
            }
        }

        @Override
        // New services discovered
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {
            if (status == BluetoothGatt.GATT_SUCCESS) {

                Runnable test = new Runnable() {
                    @Override
                    public void run() {

                        //you can improve this by using reflection
                        device = new NottiDevice(BluetoothDeviceConn.this);

                        device.addInitListener(new IDeviceInitListener() {
                            @Override
                            public void onInit() {
                                try {
                                    JSONObject object = new JSONObject();
                                    object.put(BluetoothConst.DEVICE_ADDRESS, getAddress());
                                    object.put(BluetoothConst.DEVICE_NAME, getDeviceName());

                                    ArrayList<String> values = new ArrayList<String>();
                                    values.add(object.toString());

                                    connected = true;
                                    //when device is fully intitialized broadcast service discovery
                                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_CONNECTED,
                                            values);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        device.init();
                    }
                };
                Thread testThread = new Thread(test);
                testThread.start();

            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            manager.getEventManager().set();
            if (device != null) {
                device.notifyCharacteristicWriteReceived(characteristic);
            }
        }

        @Override
        // Result of a characteristic read operation
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            manager.getEventManager().set();
            if (device != null) {
                device.notifyCharacteristicReadReceived(characteristic);
            }
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            manager.getEventManager().set();
        }

        @Override
        // Characteristic notification
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            if (device != null) {
                device.notifyCharacteristicChangeReceived(characteristic);
            }
        }
    };
}

From source file:com.github.akinaru.bleanalyzer.bluetooth.connection.BluetoothDeviceConn.java

/**
 * Build Bluetooth device connection//from w  w  w. j  av  a  2  s .  co  m
 *
 * @param address
 */
@SuppressLint("NewApi")
public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) {
    this.deviceAddr = address;
    this.deviceName = deviceName;
    this.manager = manager;

    gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

            if (newState == BluetoothProfile.STATE_CONNECTED) {

                Log.i(TAG, "Connected to GATT server.");
                Log.i(TAG, "Attempting to start service discovery:" + gatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

                connected = false;
                Log.i(TAG, "Disconnected from GATT server.");

                try {
                    JSONObject object = new JSONObject();
                    object.put(JsonConstants.BT_ADDRESS, getAddress());
                    object.put(JsonConstants.BT_DEVICE_NAME, getDeviceName());

                    ArrayList<String> values = new ArrayList<String>();
                    values.add(object.toString());

                    //when device is fully intitialized broadcast service discovery
                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCONNECTED, values);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                if (manager.getWaitingMap().containsKey(deviceAddr)) {
                    manager.getWaitingMap().get(deviceAddr).cancel(true);
                    manager.getWaitingMap().remove(deviceAddr);
                }

                if (BluetoothDeviceConn.this.gatt != null) {
                    Log.i(TAG, "connection close clean");
                    BluetoothDeviceConn.this.gatt.close();
                }
            }
        }

        @Override
        // New services discovered
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {

            if (status == BluetoothGatt.GATT_SUCCESS) {

                Runnable test = new Runnable() {
                    @Override
                    public void run() {

                        //you can improve this by using reflection
                        device = new RfduinoDevice(BluetoothDeviceConn.this);

                        device.addInitListener(new IDeviceInitListener() {
                            @Override
                            public void onInit() {
                                try {
                                    JSONObject object = new JSONObject();
                                    object.put(JsonConstants.BT_ADDRESS, getAddress());
                                    object.put(JsonConstants.BT_DEVICE_NAME, getDeviceName());

                                    ArrayList<String> values = new ArrayList<String>();
                                    values.add(object.toString());

                                    connected = true;
                                    //when device is fully intitialized broadcast service discovery
                                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_CONNECTED,
                                            values);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        device.init();
                    }
                };
                Thread testThread = new Thread(test);
                testThread.start();

            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            //manager.getEventManager().set();
            if (device != null) {
                device.notifyCharacteristicWriteReceived(characteristic);
            }
        }

        @Override
        // Result of a characteristic read operation
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            manager.getEventManager().set();
            if (device != null) {
                Log.i(TAG, "onCharacteristicRead");
                device.notifyCharacteristicReadReceived(characteristic);
            }
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            manager.getEventManager().set();
        }

        @Override
        // Characteristic notification
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            manager.getEventManager().set();
            if (device != null) {
                Log.i(TAG, "onCharacteristicChanged");
                device.notifyCharacteristicChangeReceived(characteristic);
            }
        }
    };
}

From source file:com.github.akinaru.roboticbuttonpusher.bluetooth.connection.BluetoothDeviceConn.java

/**
 * Build Bluetooth device connection/*from   ww w. jav  a2s. co m*/
 *
 * @param address
 */
@SuppressLint("NewApi")
public BluetoothDeviceConn(String address, String deviceName, final IBluetoothCustomManager manager) {
    this.deviceAddr = address;
    this.deviceName = deviceName;
    this.manager = manager;

    gattCallback = new BluetoothGattCallback() {
        @Override
        public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {

            if (newState == BluetoothProfile.STATE_CONNECTED) {

                Log.v(TAG, "Connected to GATT server.");
                Log.v(TAG, "Attempting to start service discovery:" + gatt.discoverServices());

            } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {

                connected = false;
                Log.v(TAG, "Disconnected from GATT server.");

                if (status != 133) {
                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_DISCONNECTED,
                            new ArrayList<String>());
                } else {
                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_RETRY,
                            new ArrayList<String>());
                }

                if (manager.getWaitingMap().containsKey(deviceAddr)) {
                    manager.getWaitingMap().get(deviceAddr).cancel(true);
                    manager.getWaitingMap().remove(deviceAddr);
                }

                if (BluetoothDeviceConn.this.gatt != null) {
                    Log.v(TAG, "connection close clean");
                    BluetoothDeviceConn.this.gatt.close();
                }
                if (remove) {
                    manager.getConnectionList().remove(deviceAddr);
                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_REMOVED,
                            new ArrayList<String>());
                }
            }
        }

        @Override
        // New services discovered
        public void onServicesDiscovered(BluetoothGatt gatt, int status) {

            if (status == BluetoothGatt.GATT_SUCCESS) {

                Runnable test = new Runnable() {
                    @Override
                    public void run() {

                        //you can improve this by using reflection
                        device = new RfduinoDevice(BluetoothDeviceConn.this);

                        device.addInitListener(new IDeviceInitListener() {
                            @Override
                            public void onInit() {
                                try {
                                    JSONObject object = new JSONObject();
                                    object.put(JsonConstants.BT_ADDRESS, getAddress());
                                    object.put(JsonConstants.BT_DEVICE_NAME, getDeviceName());

                                    ArrayList<String> values = new ArrayList<String>();
                                    values.add(object.toString());

                                    connected = true;
                                    //when device is fully intitialized broadcast service discovery
                                    manager.broadcastUpdateStringList(BluetoothEvents.BT_EVENT_DEVICE_CONNECTED,
                                            values);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                        device.init();
                    }
                };
                Thread testThread = new Thread(test);
                testThread.start();

            } else {
                Log.w(TAG, "onServicesDiscovered received: " + status);
            }
        }

        @Override
        public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            manager.getEventManager().set();
            if (device != null) {
                device.notifyCharacteristicWriteReceived(characteristic);
            }
        }

        @Override
        // Result of a characteristic read operation
        public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic,
                int status) {
            manager.getEventManager().set();
            if (device != null) {
                Log.v(TAG, "onCharacteristicRead");
                device.notifyCharacteristicReadReceived(characteristic);
            }
        }

        @Override
        public void onDescriptorWrite(BluetoothGatt gatt, BluetoothGattDescriptor descriptor, int status) {
            manager.getEventManager().set();
        }

        @Override
        // Characteristic notification
        public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) {
            manager.getEventManager().set();
            if (device != null) {
                Log.v(TAG, "onCharacteristicChanged");
                device.notifyCharacteristicChangeReceived(characteristic);
            }
        }
    };
}

From source file:org.physical_web.physicalweb.BluetoothSite.java

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    Log.i(TAG, "Services Discovered");
    if (status != BluetoothGatt.GATT_SUCCESS) {
        Log.e(TAG, "Service discovery failed!");
        return;//from   w w  w .  j av  a 2 s  .  c  om
    }

    try {
        mHtml = new FileOutputStream(getHtmlFile());
    } catch (FileNotFoundException e) {
        Log.e(TAG, "File not found", e);
        return;
    }

    characteristic = gatt.getService(SERVICE_UUID).getCharacteristic(CHARACTERISTIC_WEBPAGE_UUID);
    gatt.readCharacteristic(characteristic);
}

From source file:com.megster.cordova.ble.central.Peripheral.java

@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
    super.onServicesDiscovered(gatt, status);

    if (status == BluetoothGatt.GATT_SUCCESS) {
        PluginResult result = new PluginResult(PluginResult.Status.OK, this.asJSONObject(gatt));
        result.setKeepCallback(true);/*from  w  w  w.  j a v a  2s  .c o m*/
        connectCallback.sendPluginResult(result);
    } else {
        LOG.e(TAG, "Service discovery failed. status = " + status);
        connectCallback.error(this.asJSONObject());
        disconnect();
    }
}

From source file:io.v.android.libs.discovery.ble.BlePlugin.java

public BlePlugin(Context androidContext) {
    this.androidContext = androidContext;
    cachedDevices = new DeviceCache(Duration.standardMinutes(1));
    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
        return;//from   w w w.  ja v  a 2  s  .  co  m
    }

    if (!hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION)
            && !hasPermission(Manifest.permission.ACCESS_FINE_LOCATION)) {
        return;
    }
    isEnabled = true;
    bluetoothLeAdvertise = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
    bluetoothLeScanner = BluetoothAdapter.getDefaultAdapter().getBluetoothLeScanner();
    BluetoothManager manager = (BluetoothManager) androidContext.getSystemService(Context.BLUETOOTH_SERVICE);
    bluetoothGattServer = manager.openGattServer(androidContext, new BluetoothGattServerCallback() {
        @Override
        public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
            super.onConnectionStateChange(device, status, newState);
        }

        @Override
        public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset,
                BluetoothGattCharacteristic characteristic) {
            super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
            byte[] total = characteristic.getValue();
            byte[] res = {};
            // Only send MTU - 1 bytes. The first byte of all packets is the op code.
            if (offset < total.length) {
                int finalByte = offset + MTU - 1;
                if (finalByte > total.length) {
                    finalByte = total.length;
                }
                res = Arrays.copyOfRange(total, offset, finalByte);
                bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, res);
            } else {
                // This should probably be an error, but a bug in the paypal/gatt code causes an
                // infinite loop if this returns an error rather than the empty value.
                bluetoothGattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, 0, res);
            }
        }
    });
}