Java Code Examples for android.bluetooth.BluetoothAdapter#ACTION_DISCOVERY_FINISHED

The following examples show how to use android.bluetooth.BluetoothAdapter#ACTION_DISCOVERY_FINISHED . 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: BluetoothAdapterSnippet.java    From mobly-bundled-snippets with Apache License 2.0 5 votes vote down vote up
@Rpc(description = "Cancel ongoing bluetooth discovery.")
public void btCancelDiscovery() throws BluetoothAdapterSnippetException {
    if (!mBluetoothAdapter.isDiscovering()) {
        Log.d("No ongoing bluetooth discovery.");
        return;
    }
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
    mIsDiscoveryFinished = false;
    BroadcastReceiver receiver = new BluetoothScanReceiver();
    mContext.registerReceiver(receiver, filter);
    try {
        if (!mBluetoothAdapter.cancelDiscovery()) {
            throw new BluetoothAdapterSnippetException(
                    "Failed to initiate to cancel bluetooth discovery.");
        }
        if (!Utils.waitUntil(() -> mIsDiscoveryFinished, 120)) {
            throw new BluetoothAdapterSnippetException(
                    "Failed to get discovery results after 2 mins, timeout!");
        }
    } finally {
        mContext.unregisterReceiver(receiver);
    }
}
 
Example 4
Source File: BroadcastReceiverDelegator.java    From blue-pair with MIT License 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    Log.d(TAG, "Incoming intent : " + action);
    switch (action) {
        case BluetoothDevice.ACTION_FOUND :
            // Discovery has found a device. Get the BluetoothDevice
            // object and its info from the Intent.
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            Log.d(TAG, "Device discovered! " + BluetoothController.deviceToString(device));
            listener.onDeviceDiscovered(device);
            break;
        case BluetoothAdapter.ACTION_DISCOVERY_FINISHED :
            // Discovery has ended.
            Log.d(TAG, "Discovery ended.");
            listener.onDeviceDiscoveryEnd();
            break;
        case BluetoothAdapter.ACTION_STATE_CHANGED :
            // Discovery state changed.
            Log.d(TAG, "Bluetooth state changed.");
            listener.onBluetoothStatusChanged();
            break;
        case BluetoothDevice.ACTION_BOND_STATE_CHANGED :
            // Pairing state has changed.
            Log.d(TAG, "Bluetooth bonding state changed.");
            listener.onDevicePairingEnded();
            break;
        default :
            // Does nothing.
            break;
    }
}
 
Example 5
Source File: MainActivity.java    From BlueToothEatPhone with Apache License 2.0 5 votes vote down vote up
private void initDate() {
        mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        /**注册搜索蓝牙receiver*/
        mFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        mFilter.addAction(BluetoothDevice.ACTION_FOUND);
        mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
        registerReceiver(mReceiver, mFilter);
        /**注册配对状态改变监听器*/
//        IntentFilter mFilter = new IntentFilter();
//        mFilter.addAction(BluetoothDevice.ACTION_BOND_STATE_CHANGED);
//        mFilter.addAction(BluetoothDevice.ACTION_FOUND);
//        registerReceiver(mReceiver, mFilter);

        getBondedDevices();
    }
 
Example 6
Source File: SKBluetooth.java    From SensingKit-Android with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void registerLocalBroadcastManager() {

        // Register receivers for ACTION_FOUND and ACTION_DISCOVERY_FINISHED
        IntentFilter foundFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
        IntentFilter finishedFilter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
        mApplicationContext.registerReceiver(mReceiver, foundFilter);
        mApplicationContext.registerReceiver(mReceiver, finishedFilter);
    }
 
Example 7
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;
    }
}