Java Code Examples for android.bluetooth.BluetoothAdapter#ACTION_DISCOVERY_STARTED

The following examples show how to use android.bluetooth.BluetoothAdapter#ACTION_DISCOVERY_STARTED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BluetoothStateReceiver.java    From SimpleBluetoothLibrary with Apache License 2.0 6 votes vote down vote up
/**
 * Register this receiver with a series of intent filters.
 * @param context the context that will register the receiver.
 * @param callback the callback to notify when there are changes.
 * @return an instance of the {@code BluetoothStateReceiver}
 */
public static BluetoothStateReceiver register(Context context, Callback callback) {

    //create the intent filters.
    IntentFilter connected = new IntentFilter(BluetoothDevice.ACTION_ACL_CONNECTED);
    IntentFilter disconnectRequest = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECT_REQUESTED);
    IntentFilter disconnected = new IntentFilter(BluetoothDevice.ACTION_ACL_DISCONNECTED);
    IntentFilter discoveryFinished = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    IntentFilter discoveryStarted = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
    BluetoothStateReceiver bluetoothStateReceiver = new BluetoothStateReceiver(callback);

    //register for each filter.
    context.registerReceiver(bluetoothStateReceiver, connected);
    context.registerReceiver(bluetoothStateReceiver, disconnectRequest);
    context.registerReceiver(bluetoothStateReceiver, disconnected);
    context.registerReceiver(bluetoothStateReceiver, discoveryFinished);
    context.registerReceiver(bluetoothStateReceiver, discoveryStarted);
    return bluetoothStateReceiver;
}
 
Example 2
Source File: AvailableDevicesFragment.java    From wearmouse with Apache License 2.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (getContext() == null) {
        Log.w(TAG, "BluetoothScanReceiver context disappeared");
        return;
    }

    final String action = intent.getAction();
    final BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

    switch (action == null ? "" : action) {
        case BluetoothDevice.ACTION_FOUND:
            if (hidDeviceProfile.isProfileSupported(device)) {
                addAvailableDevice(device);
            }
            break;
        case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
            initiateScanDevices.setEnabled(false);
            initiateScanDevices.setTitle(R.string.pref_bluetoothScan_scanning);
            break;
        case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
            initiateScanDevices.setEnabled(true);
            initiateScanDevices.setTitle(R.string.pref_bluetoothScan);
            break;
        case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
            updateAvailableDevice(device);
            break;
        case BluetoothDevice.ACTION_NAME_CHANGED:
            BluetoothDevicePreference pref = findDevicePreference(device);
            if (pref != null) {
                pref.updateName();
            }
            break;
        default: // fall out
    }
}
 
Example 3
Source File: BluetoothReceiver.java    From ClassicBluetooth with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    BluetoothDevice dev;
    int state;
    if (action == null) {
        return;
    }
    switch (action) {
        /**
         * 蓝牙开关状态
         * int STATE_OFF = 10; //蓝牙关闭
         * int STATE_ON = 12; //蓝牙打开
         * int STATE_TURNING_OFF = 13; //蓝牙正在关闭
         * int STATE_TURNING_ON = 11; //蓝牙正在打开
         */
        case BluetoothAdapter.ACTION_STATE_CHANGED:
            state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
            mCallback.onStateSwitch(state);
            break;
        /**
         * 蓝牙开始搜索
         */
        case BluetoothAdapter.ACTION_DISCOVERY_STARTED:
            CbtLogs.i("蓝牙开始搜索");
            break;
        /**
         * 蓝牙搜索结束
         */
        case BluetoothAdapter.ACTION_DISCOVERY_FINISHED:
            CbtLogs.i("蓝牙扫描结束");
            mCallback.onScanStop();
            break;
        /**
         * 发现新设备
         */
        case BluetoothDevice.ACTION_FOUND:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            mCallback.onFindDevice(dev);
            break;
        /**
         * 设备配对状态改变
         * int BOND_NONE = 10; //配对没有成功
         * int BOND_BONDING = 11; //配对中
         * int BOND_BONDED = 12; //配对成功
         */
        case BluetoothDevice.ACTION_BOND_STATE_CHANGED:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            CbtLogs.i("设备配对状态改变:" + dev.getBondState());
            break;
        /**
         * 设备建立连接
         * int STATE_DISCONNECTED = 0; //未连接
         * int STATE_CONNECTING = 1; //连接中
         * int STATE_CONNECTED = 2; //连接成功
         */
        case BluetoothDevice.ACTION_ACL_CONNECTED:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            CbtLogs.i("设备建立连接:" + dev.getBondState());
            mCallback.onConnect(dev);
            break;
        /**
         * 设备断开连接
         */
        case BluetoothDevice.ACTION_ACL_DISCONNECTED:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            // mCallback.onConnect(dev.getBondState(), dev);
            break;
        /**
         * 本地蓝牙适配器
         * BluetoothAdapter连接状态
         */
        case BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            CbtLogs.i("STATE: " + intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0));
            CbtLogs.i("BluetoothDevice: " + dev.getName() + ", " + dev.getAddress());
            break;
        /**
         * 提供用于手机的蓝牙耳机支持
         * BluetoothHeadset连接状态
         */
        case BluetoothHeadset.ACTION_CONNECTION_STATE_CHANGED:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            CbtLogs.i("STATE: " + intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, 0));
            CbtLogs.i("BluetoothDevice: " + dev.getName() + ", " + dev.getAddress());
            break;
        /**
         * 定义高质量音频可以从一个设备通过蓝牙连接传输到另一个设备
         * BluetoothA2dp连接状态
         */
        case BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED:
            dev = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            CbtLogs.i("STATE: " + intent.getIntExtra(BluetoothHeadset.EXTRA_STATE, 0));
            CbtLogs.i("BluetoothDevice: " + dev.getName() + ", " + dev.getAddress());
            break;
        default:
            break;
    }
}