Search in sources :

Example 41 with BluetoothAdapter

use of android.bluetooth.BluetoothAdapter in project android_packages_apps_Settings by LineageOS.

the class DevelopmentSettings method writeBtHciSnoopLogOptions.

private void writeBtHciSnoopLogOptions() {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    SystemProperties.set(BLUETOOTH_BTSNOOP_ENABLE_PROPERTY, Boolean.toString(mBtHciSnoopLog.isChecked()));
}
Also used : BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 42 with BluetoothAdapter

use of android.bluetooth.BluetoothAdapter in project fdroidclient by f-droid.

the class SwapWorkflowActivity method startBluetoothSwap.

/**
 * The process for setting up bluetooth is as follows:
 *  * Assume we have bluetooth available (otherwise the button which allowed us to start
 *    the bluetooth process should not have been available).
 *  * Ask user to enable (if not enabled yet).
 *  * Start bluetooth server socket.
 *  * Enable bluetooth discoverability, so that people can connect to our server socket.
 *
 * Note that this is a little different than the usual process for bluetooth _clients_, which
 * involves pairing and connecting with other devices.
 */
public void startBluetoothSwap() {
    Utils.debugLog(TAG, "Initiating Bluetooth swap, will ensure the Bluetooth devices is enabled and discoverable before starting server.");
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    if (adapter != null) {
        if (adapter.isEnabled()) {
            Utils.debugLog(TAG, "Bluetooth enabled, will check if device is discoverable with device.");
            ensureBluetoothDiscoverableThenStart();
        } else {
            Utils.debugLog(TAG, "Bluetooth disabled, asking user to enable it.");
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent, REQUEST_BLUETOOTH_ENABLE_FOR_SWAP);
        }
    }
}
Also used : PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 43 with BluetoothAdapter

use of android.bluetooth.BluetoothAdapter in project fdroidclient by f-droid.

the class BluetoothServer method run.

@Override
public void run() {
    isRunning = true;
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    try {
        serverSocket = adapter.listenUsingInsecureRfcommWithServiceRecord("FDroid App Swap", BluetoothConstants.fdroidUuid());
    } catch (IOException e) {
        Log.e(TAG, "Error starting Bluetooth server socket, will stop the server now", e);
        swap.stop();
        isRunning = false;
        return;
    }
    while (true) {
        if (isInterrupted()) {
            Utils.debugLog(TAG, "Server stopped so will terminate loop looking for client connections.");
            break;
        }
        try {
            BluetoothSocket clientSocket = serverSocket.accept();
            if (clientSocket != null) {
                if (isInterrupted()) {
                    Utils.debugLog(TAG, "Server stopped after socket accepted from client, but before initiating connection.");
                    break;
                }
                ClientConnection client = new ClientConnection(clientSocket, webRoot);
                client.start();
                clients.add(client);
            }
        } catch (IOException e) {
            Log.e(TAG, "Error receiving client connection over Bluetooth server socket, will continue listening for other clients", e);
        }
    }
    isRunning = false;
}
Also used : IOException(java.io.IOException) BluetoothSocket(android.bluetooth.BluetoothSocket) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 44 with BluetoothAdapter

use of android.bluetooth.BluetoothAdapter in project xDrip by NightscoutFoundation.

the class SystemStatus method forgetDeviceListener.

private void forgetDeviceListener() {
    forget_device.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (mBluetoothManager != null && ActiveBluetoothDevice.first() != null) {
                final BluetoothAdapter bluetoothAdapter = mBluetoothManager.getAdapter();
                if (bluetoothAdapter != null) {
                    for (BluetoothDevice bluetoothDevice : bluetoothAdapter.getBondedDevices()) {
                        if (bluetoothDevice.getAddress().compareTo(ActiveBluetoothDevice.first().address) == 0) {
                            try {
                                Method m = bluetoothDevice.getClass().getMethod("removeBond", (Class[]) null);
                                m.invoke(bluetoothDevice, (Object[]) null);
                                notes.append("\n- Bluetooth unbonded, if using share tell it to forget your device.");
                                notes.append("\n- Scan for devices again to set connection back up!");
                            } catch (Exception e) {
                                Log.e("SystemStatus", e.getMessage(), e);
                            }
                        }
                    }
                    ActiveBluetoothDevice.forget();
                    bluetoothAdapter.disable();
                    mHandler.postDelayed(new Runnable() {

                        public void run() {
                            bluetoothAdapter.enable();
                            set_current_values();
                            mHandler2.postDelayed(new Runnable() {

                                public void run() {
                                    CollectionServiceStarter.restartCollectionService(getApplicationContext());
                                    set_current_values();
                                }
                            }, 5000);
                        }
                    }, 1000);
                }
            }
            String collection_method = prefs.getString("dex_collection_method", "BluetoothWixel");
            if (collection_method.compareTo("DexcomG5") == 0) {
                Transmitter defaultTransmitter = new Transmitter(prefs.getString("dex_txid", "ABCDEF"));
                mBluetoothAdapter = mBluetoothManager.getAdapter();
                Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
                if ((pairedDevices != null) && (pairedDevices.size() > 0)) {
                    for (BluetoothDevice device : pairedDevices) {
                        if (device.getName() != null) {
                            String transmitterIdLastTwo = Extensions.lastTwoCharactersOfString(defaultTransmitter.transmitterId);
                            String deviceNameLastTwo = Extensions.lastTwoCharactersOfString(device.getName());
                            if (transmitterIdLastTwo.equals(deviceNameLastTwo)) {
                                try {
                                    Method m = device.getClass().getMethod("removeBond", (Class[]) null);
                                    m.invoke(device, (Object[]) null);
                                    notes.append("\nG5 Transmitter unbonded, switch device mode to prevent re-pairing to G5.");
                                } catch (Exception e) {
                                    Log.e("SystemStatus", e.getMessage(), e);
                                }
                            }
                        }
                    }
                }
            }
        }
    });
}
Also used : Set(java.util.Set) BluetoothDevice(android.bluetooth.BluetoothDevice) ActiveBluetoothDevice(com.eveningoutpost.dexdrip.Models.ActiveBluetoothDevice) Transmitter(com.eveningoutpost.dexdrip.G5Model.Transmitter) Method(java.lang.reflect.Method) View(android.view.View) TextView(android.widget.TextView) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 45 with BluetoothAdapter

use of android.bluetooth.BluetoothAdapter in project xDrip by NightscoutFoundation.

the class Ob1G5CollectionService method unBond.

public synchronized void unBond() {
    UserError.Log.d(TAG, "unBond() start");
    if (transmitterMAC == null)
        return;
    final BluetoothAdapter mBluetoothAdapter = ((BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE)).getAdapter();
    final Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            if (device.getAddress() != null) {
                if (device.getAddress().equals(transmitterMAC)) {
                    try {
                        UserError.Log.e(TAG, "removingBond: " + transmitterMAC);
                        Method m = device.getClass().getMethod("removeBond", (Class[]) null);
                        m.invoke(device, (Object[]) null);
                    } catch (Exception e) {
                        UserError.Log.e(TAG, e.getMessage(), e);
                    }
                }
            }
        }
    }
    UserError.Log.d(TAG, "unBond() finished");
}
Also used : BluetoothDevice(android.bluetooth.BluetoothDevice) BluetoothManager(android.bluetooth.BluetoothManager) Method(java.lang.reflect.Method) BluetoothAdapter(android.bluetooth.BluetoothAdapter) BleScanException(com.polidea.rxandroidble.exceptions.BleScanException)

Aggregations

BluetoothAdapter (android.bluetooth.BluetoothAdapter)184 Intent (android.content.Intent)27 BluetoothDevice (android.bluetooth.BluetoothDevice)22 BluetoothManager (android.bluetooth.BluetoothManager)16 BluetoothPan (android.bluetooth.BluetoothPan)15 IntentFilter (android.content.IntentFilter)15 LocationManager (android.location.LocationManager)15 BluetoothProfile (android.bluetooth.BluetoothProfile)13 Location (android.location.Location)12 LocationListener (android.location.LocationListener)12 Bundle (android.os.Bundle)12 TextView (android.widget.TextView)10 BluetoothLeScanner (android.bluetooth.le.BluetoothLeScanner)9 ScanSettings (android.bluetooth.le.ScanSettings)9 ScanCallback (android.bluetooth.le.ScanCallback)8 ScanResult (android.bluetooth.le.ScanResult)8 Method (java.lang.reflect.Method)8 List (java.util.List)8 Activity (android.app.Activity)7 ScanFilter (android.bluetooth.le.ScanFilter)7