Search in sources :

Example 6 with BluetoothManager

use of android.bluetooth.BluetoothManager in project Gadgetbridge by Freeyourgadget.

the class DiscoveryActivity method checkBluetoothAvailable.

private boolean checkBluetoothAvailable() {
    BluetoothManager bluetoothService = (BluetoothManager) getSystemService(BLUETOOTH_SERVICE);
    if (bluetoothService == null) {
        LOG.warn("No bluetooth available");
        this.adapter = null;
        return false;
    }
    BluetoothAdapter adapter = bluetoothService.getAdapter();
    if (adapter == null) {
        LOG.warn("No bluetooth available");
        this.adapter = null;
        return false;
    }
    if (!adapter.isEnabled()) {
        LOG.warn("Bluetooth not enabled");
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivity(enableBtIntent);
        this.adapter = null;
        return false;
    }
    this.adapter = adapter;
    return true;
}
Also used : BluetoothManager(android.bluetooth.BluetoothManager) Intent(android.content.Intent) BluetoothAdapter(android.bluetooth.BluetoothAdapter)

Example 7 with BluetoothManager

use of android.bluetooth.BluetoothManager in project Gadgetbridge by Freeyourgadget.

the class PebbleGATTServer method initialize.

boolean initialize() {
    BluetoothManager bluetoothManager = (BluetoothManager) mContext.getSystemService(Context.BLUETOOTH_SERVICE);
    mBluetoothGattServer = bluetoothManager.openGattServer(mContext, this);
    if (mBluetoothGattServer == null) {
        return false;
    }
    BluetoothGattService pebbleGATTService = new BluetoothGattService(SERVER_SERVICE, BluetoothGattService.SERVICE_TYPE_PRIMARY);
    pebbleGATTService.addCharacteristic(new BluetoothGattCharacteristic(READ_CHARACTERISTICS, BluetoothGattCharacteristic.PROPERTY_READ, BluetoothGattCharacteristic.PERMISSION_READ));
    writeCharacteristics = new BluetoothGattCharacteristic(WRITE_CHARACTERISTICS, BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE | BluetoothGattCharacteristic.PROPERTY_NOTIFY, BluetoothGattCharacteristic.PERMISSION_WRITE);
    writeCharacteristics.addDescriptor(new BluetoothGattDescriptor(CHARACTERISTICS_CONFIGURATION_DESCRIPTOR, BluetoothGattDescriptor.PERMISSION_WRITE));
    pebbleGATTService.addCharacteristic(writeCharacteristics);
    mBluetoothGattServer.addService(pebbleGATTService);
    return true;
}
Also used : BluetoothManager(android.bluetooth.BluetoothManager) BluetoothGattService(android.bluetooth.BluetoothGattService) BluetoothGattDescriptor(android.bluetooth.BluetoothGattDescriptor) BluetoothGattCharacteristic(android.bluetooth.BluetoothGattCharacteristic)

Example 8 with BluetoothManager

use of android.bluetooth.BluetoothManager in project android_wear_for_ios by shiitakeo.

the class BLEService method onStart.

@TargetApi(20)
@Override
public void onStart(Intent intent, int startID) {
    Log.d(TAG_LOG, "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    IntentFilter intent_filter = new IntentFilter();
    intent_filter.addAction(action_positive);
    intent_filter.addAction(action_negative);
    intent_filter.addAction(action_delete);
    intent_filter.addAction(action_renotify);
    registerReceiver(message_receiver, intent_filter);
    vib = (Vibrator) getSystemService(VIBRATOR_SERVICE);
    notificationManager = NotificationManagerCompat.from(getApplicationContext());
    packet_processor = new PacketProcessor();
    icon_image_manager = new IconImageManager();
    if (bluetooth_gatt != null) {
        bluetooth_gatt.disconnect();
        bluetooth_gatt.close();
        bluetooth_gatt = null;
    }
    if (bluetooth_adapter != null) {
        bluetooth_adapter = null;
    }
    if (api_level >= 21) {
        if (le_scanner != null) {
            Log.d(TAG_LOG, "status: ble reset");
            stop_le_scanner();
        }
    }
    is_connect = false;
    is_subscribed_characteristics = false;
    iphone_uuid = "";
    start_time = 0;
    is_time = false;
    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    bluetooth_adapter = bluetoothManager.getAdapter();
    // Checks if Bluetooth is supported on the device.
    if (bluetooth_adapter == null) {
        Log.d(TAG_LOG, "ble adapter is null");
        return;
    }
    if (api_level >= 21) {
        Log.d(TAG_LOG, "start BLE scan @ lescan");
        start_le_scanner();
    } else {
        Log.d(TAG_LOG, "start BLE scan @ BluetoothAdapter");
        bluetooth_adapter.startLeScan(le_scan_callback);
    }
    //music control notification
    Intent __intent = new Intent(this, MusicControlActivity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, __intent, 0);
    Intent _intent_delete = new Intent();
    _intent_delete.setAction(action_renotify);
    PendingIntent _delete_action = PendingIntent.getBroadcast(getApplicationContext(), notification_id, _intent_delete, PendingIntent.FLAG_ONE_SHOT);
    Notification.Builder notificationBuilder = new Notification.Builder(this).setSmallIcon(R.drawable.whatsapp).setContentTitle("music💿").setContentIntent(pendingIntent).addAction(R.drawable.resume, "Controller Open", pendingIntent).setLocalOnly(true).setDeleteIntent(_delete_action).extend(new Notification.WearableExtender().setContentAction(0).setHintHideIcon(true));
    Notification _notification = notificationBuilder.build();
    //        _notification.flags = _notification.flags | Notification.FLAG_ONGOING_EVENT;
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(id_music_control, _notification);
    notification_id++;
}
Also used : IntentFilter(android.content.IntentFilter) NotificationManager(android.app.NotificationManager) BluetoothManager(android.bluetooth.BluetoothManager) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) Notification(android.app.Notification) TargetApi(android.annotation.TargetApi)

Example 9 with BluetoothManager

use of android.bluetooth.BluetoothManager in project android_wear_for_ios by shiitakeo.

the class MusicControlActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_music_control);
    Log.d(TAG_LOG, "-=-=-=-=-=-=-=-= onCreate @ MusicControlActivity -=-=-=-=-=-=-=-=-=");
    IntentFilter intent_filter = new IntentFilter();
    intent_filter.addAction(set_artist_info);
    intent_filter.addAction(set_title_info);
    registerReceiver(message_receiver, intent_filter);
    if (bluetooth_gatt != null) {
        bluetooth_gatt.disconnect();
        bluetooth_gatt.close();
        bluetooth_gatt = null;
    }
    if (bluetooth_adapter != null) {
        bluetooth_adapter = null;
    }
    if (api_level >= 21) {
        if (le_scanner != null) {
            Log.d(TAG_LOG, "status: ble reset");
            stop_le_scanner();
        }
    }
    is_connect = false;
    iphone_uuid = "";
    // Initializes a Bluetooth adapter.  For API level 18 and above, get a reference to
    // BluetoothAdapter through BluetoothManager.
    final BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
    bluetooth_adapter = bluetoothManager.getAdapter();
    // Checks if Bluetooth is supported on the device.
    if (bluetooth_adapter == null) {
        Log.d(TAG_LOG, "ble adapter is null");
        return;
    }
    if (api_level >= 21) {
        Log.d(TAG_LOG, "start BLE scan @ lescan");
        start_le_scanner();
    } else {
        Log.d(TAG_LOG, "start BLE scan @ BluetoothAdapter");
        bluetooth_adapter.startLeScan(le_scan_callback);
    }
}
Also used : IntentFilter(android.content.IntentFilter) BluetoothManager(android.bluetooth.BluetoothManager)

Example 10 with BluetoothManager

use of android.bluetooth.BluetoothManager in project android-beacon-library by AltBeacon.

the class CycledLeScanner method getBluetoothAdapter.

protected BluetoothAdapter getBluetoothAdapter() {
    try {
        if (mBluetoothAdapter == null) {
            // Initializes Bluetooth adapter.
            final BluetoothManager bluetoothManager = (BluetoothManager) mContext.getApplicationContext().getSystemService(Context.BLUETOOTH_SERVICE);
            mBluetoothAdapter = bluetoothManager.getAdapter();
            if (mBluetoothAdapter == null) {
                LogManager.w(TAG, "Failed to construct a BluetoothAdapter");
            }
        }
    } catch (SecurityException e) {
        // Thrown by Samsung Knox devices if bluetooth access denied for an app
        LogManager.e(TAG, "Cannot consruct bluetooth adapter.  Security Exception");
    }
    return mBluetoothAdapter;
}
Also used : BluetoothManager(android.bluetooth.BluetoothManager)

Aggregations

BluetoothManager (android.bluetooth.BluetoothManager)11 Intent (android.content.Intent)4 BluetoothAdapter (android.bluetooth.BluetoothAdapter)3 IntentFilter (android.content.IntentFilter)3 TargetApi (android.annotation.TargetApi)2 PendingIntent (android.app.PendingIntent)2 Notification (android.app.Notification)1 NotificationManager (android.app.NotificationManager)1 BluetoothGattCharacteristic (android.bluetooth.BluetoothGattCharacteristic)1 BluetoothGattDescriptor (android.bluetooth.BluetoothGattDescriptor)1 BluetoothGattService (android.bluetooth.BluetoothGattService)1 Handler (android.os.Handler)1