Java Code Examples for android.bluetooth.BluetoothClass

The following examples show how to use android.bluetooth.BluetoothClass. These examples are extracted from open source projects. 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 Project: mytracks   Source File: BluetoothDeviceUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Populates the device names and the device addresses with all the suitable
 * bluetooth devices.
 * 
 * @param bluetoothAdapter the bluetooth adapter
 * @param deviceNames list of device names
 * @param deviceAddresses list of device addresses
 */
public static void populateDeviceLists(
    BluetoothAdapter bluetoothAdapter, List<String> deviceNames, List<String> deviceAddresses) {
  // Ensure the bluetooth adapter is not in discovery mode.
  bluetoothAdapter.cancelDiscovery();

  Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
  for (BluetoothDevice device : pairedDevices) {
    BluetoothClass bluetoothClass = device.getBluetoothClass();
    if (bluetoothClass != null) {
      // Not really sure what we want, but I know what we don't want.
      switch (bluetoothClass.getMajorDeviceClass()) {
        case BluetoothClass.Device.Major.COMPUTER:
        case BluetoothClass.Device.Major.PHONE:
          break;
        default:
          deviceAddresses.add(device.getAddress());
          deviceNames.add(device.getName());
      }
    }
  }
}
 
Example 2
Source Project: scangon   Source File: ScanGunKeyEventHelper.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 扫描枪是否连接
 * @return
 */
public boolean hasScanGun() {

    if (mBluetoothAdapter == null) {
        return false;
    }

    Set<BluetoothDevice> blueDevices = mBluetoothAdapter.getBondedDevices();

    if (blueDevices == null || blueDevices.size() <= 0) {
        return false;
    }

    for (Iterator<BluetoothDevice> iterator = blueDevices.iterator(); iterator.hasNext(); ) {
        BluetoothDevice bluetoothDevice = iterator.next();

        if (bluetoothDevice.getBluetoothClass().getMajorDeviceClass() == BluetoothClass.Device.Major.PERIPHERAL) {
            mDeviceName = bluetoothDevice.getName();
            return isInputDeviceExist(mDeviceName);
        }

    }

    return false;

}
 
Example 3
Source Project: nxt-remote-control   Source File: ChooseDeviceActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    
    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        if ((device.getBondState() != BluetoothDevice.BOND_BONDED) && (device.getBluetoothClass().getDeviceClass() == BluetoothClass.Device.TOY_ROBOT)) {
            mNewDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress());
            findViewById(R.id.title_new_devices).setVisibility(View.VISIBLE);
            findViewById(R.id.no_devices).setVisibility(View.GONE);
        }
    } else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED.equals(action)) {
        setProgressBarIndeterminateVisibility(false);
        setTitle("Select device");
        findViewById(R.id.button_scan).setVisibility(View.VISIBLE);
    }
}
 
Example 4
Source Project: wearmouse   Source File: BluetoothDevicePreference.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
    super.onPrepareDialogBuilder(builder);

    connectionState = hidDeviceProfile.getConnectionState(device);
    if (connectionState == BluetoothProfile.STATE_CONNECTED
            || connectionState == BluetoothProfile.STATE_CONNECTING) {
        builder.setPositiveButton(
                R.string.pref_bluetooth_disconnect,
                (dialog, which) -> hidDataSender.requestConnect(null));
    } else {
        builder.setPositiveButton(
                R.string.pref_bluetooth_connect,
                (dialog, which) -> hidDataSender.requestConnect(device));
    }

    if (connectionState == BluetoothProfile.STATE_CONNECTED) {
        builder.setNegativeButton(
                R.string.pref_bluetooth_select,
                (dialog, which) -> hidDataSender.requestConnect(device));
    } else if (device.getBluetoothClass() != null
            && device.getBluetoothClass().getMajorDeviceClass()
                    != BluetoothClass.Device.Major.PHONE) {
        builder.setNegativeButton(
                R.string.pref_bluetooth_forget, (dialog, which) -> requestUnpair());
    }
}
 
Example 5
Source Project: wearmouse   Source File: BluetoothDevicePreference.java    License: Apache License 2.0 5 votes vote down vote up
private void updateClass() {
    if (device.getBluetoothClass() == null) {
        return;
    }

    switch (device.getBluetoothClass().getDeviceClass()) {
        case BluetoothClass.Device.PHONE_CELLULAR:
        case BluetoothClass.Device.PHONE_SMART:
        case BluetoothClass.Device.PHONE_UNCATEGORIZED:
            setIcon(R.drawable.ic_bt_phone);
            break;

        case BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES:
        case BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET:
        case BluetoothClass.Device.AUDIO_VIDEO_LOUDSPEAKER:
            setIcon(R.drawable.ic_bt_headset);
            break;

        case BluetoothClass.Device.WEARABLE_WRIST_WATCH:
            setIcon(R.drawable.ic_bt_watch);
            break;

        case BluetoothClass.Device.WEARABLE_GLASSES:
            setIcon(R.drawable.ic_bt_glass);
            break;
        default: // fall out
    }
    notifyChanged();
}
 
Example 6
@Override
public void onReceive(Context context, Intent intent) {
  if (intent == null) return;
  Log.i(TAG, "onReceive");

  synchronized (LOCK) {
    if (getScoChangeIntent().equals(intent.getAction())) {
      int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);

      if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
        if (bluetoothHeadset != null) {
          List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();

          for (BluetoothDevice device : devices) {
            if (bluetoothHeadset.isAudioConnected(device)) {
              int deviceClass = device.getBluetoothClass().getDeviceClass();

              if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE ||
                  deviceClass == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO ||
                  deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET)
              {
                scoConnection = ScoConnection.CONNECTED;

                if (wantsConnection) {
                  AudioManager audioManager = ServiceUtil.getAudioManager(context);
                  audioManager.setBluetoothScoOn(true);
                }
              }
            }
          }

        }
      }
    }
  }

  handleBluetoothStateChange();
}
 
Example 7
Source Project: bitgatt   Source File: GattUtils.java    License: Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Will take an int from the Major Device Class and translate it into a string
 *
 * @param majDevClass The major device class numerical value
 * @return The string value
 */

public String getMajDevClassDescription(int majDevClass) {
    switch (majDevClass) {
        case BluetoothClass.Device.Major.AUDIO_VIDEO:
            return "AUDIO_VIDEO";
        case BluetoothClass.Device.Major.COMPUTER:
            return "COMPUTER";
        case BluetoothClass.Device.Major.HEALTH:
            return "HEALTH";
        case BluetoothClass.Device.Major.IMAGING:
            return "IMAGING";
        case BluetoothClass.Device.Major.MISC:
            return "MISC";
        case BluetoothClass.Device.Major.NETWORKING:
            return "NETWORKING";
        case BluetoothClass.Device.Major.PERIPHERAL:
            return "PERIPHERAL";
        case BluetoothClass.Device.Major.PHONE:
            return "PHONE";
        case BluetoothClass.Device.Major.TOY:
            return "TOY";
        case BluetoothClass.Device.Major.UNCATEGORIZED:
            return "UNCATEGORIZED";
        case BluetoothClass.Device.Major.WEARABLE:
            return "WEARABLE";
        default:
            return "UNKNOWN" + Integer.toString(majDevClass);
    }
}
 
Example 8
@Override
public void onReceive(Context context, Intent intent) {
    if (intent == null) {
        return;
    }
    Log.w(TAG, "onReceive");

    synchronized (LOCK) {
        if (getScoChangeIntent().equals(intent.getAction())) {
            int status = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE, AudioManager.SCO_AUDIO_STATE_ERROR);

            if (status == AudioManager.SCO_AUDIO_STATE_CONNECTED && bluetoothHeadset != null) {
                List<BluetoothDevice> devices = bluetoothHeadset.getConnectedDevices();

                for (BluetoothDevice device : devices) {
                    if (bluetoothHeadset.isAudioConnected(device)) {
                        int deviceClass = device.getBluetoothClass().getDeviceClass();

                        if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE ||
                                deviceClass == BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO ||
                                deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) {
                            scoConnection = ScoConnection.CONNECTED;

                            if (wantsConnection) {
                                AudioManager audioManager = AppUtil.INSTANCE.getAudioManager(context);
                                audioManager.setBluetoothScoOn(true);
                            }
                        }
                    }
                }
            }
        }
    }

    handleBluetoothStateChange();
}
 
Example 9
Source Project: sealrtc-android   Source File: BluetoothUtil.java    License: MIT License 5 votes vote down vote up
private static String getStyleContent(int styleMajor) {
    String content = "未知....";
    switch (styleMajor) {
        case BluetoothClass.Device.Major.AUDIO_VIDEO: // 音频设备
            content = "音配设备";
            break;
        case BluetoothClass.Device.Major.COMPUTER: // 电脑
            content = "电脑";
            break;
        case BluetoothClass.Device.Major.HEALTH: // 健康状况
            content = "健康状况";
            break;
        case BluetoothClass.Device.Major.IMAGING: // 镜像,映像
            content = "镜像";
            break;
        case BluetoothClass.Device.Major.MISC: // 麦克风
            content = "麦克风";
            break;
        case BluetoothClass.Device.Major.NETWORKING: // 网络
            content = "网络";
            break;
        case BluetoothClass.Device.Major.PERIPHERAL: // 外部设备
            content = "外部设备";
            break;
        case BluetoothClass.Device.Major.PHONE: // 电话
            content = "电话";
            break;
        case BluetoothClass.Device.Major.TOY: // 玩具
            content = "玩具";
            break;
        case BluetoothClass.Device.Major.UNCATEGORIZED: // 未知的
            content = "未知的";
            break;
        case BluetoothClass.Device.Major.WEARABLE: // 穿戴设备
            content = "穿戴设备";
            break;
    }
    return content;
}
 
Example 10
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

    View view = inflater.inflate(R.layout.view_add_bluetooth_server, container);

    ListView listViewBtPairedPCs = (ListView) view.findViewById(R.id.listViewBtPairedPCs);

    ArrayList<BluetoothDevice> pairedBluetoothList = new ArrayList<>();

    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            if(device.getBluetoothClass().getMajorDeviceClass() == BluetoothClass.Device.Major.COMPUTER) {
                pairedBluetoothList.add(device);
            }
        }
        if (pairedBluetoothList.size() == 0) {
            Toast.makeText(getActivity().getApplicationContext(), R.string.bluetooth_no_paired_found, Toast.LENGTH_LONG).show();
            return null;
        }
    } else {
        Toast.makeText(getActivity().getApplicationContext(), R.string.bluetooth_no_paired_found, Toast.LENGTH_LONG).show();
        return null;
    }
    BluetoothPairedDevicesAdapter adapter = new BluetoothPairedDevicesAdapter(getActivity(), pairedBluetoothList, this);
    listViewBtPairedPCs.setAdapter(adapter);
    return view;
}
 
Example 11
Source Project: CSipSimple   Source File: BluetoothUtils8.java    License: GNU General Public License v3.0 5 votes vote down vote up
public boolean canBluetooth() {
	// Detect if any bluetooth a device is available for call
	if (bluetoothAdapter == null) {
	    // Device does not support Bluetooth
		return false;
	}
	boolean hasConnectedDevice = false;
	//If bluetooth is on
	if(bluetoothAdapter.isEnabled()) {
		
		//We get all bounded bluetooth devices
		// bounded is not enough, should search for connected devices....
		Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();
		for(BluetoothDevice device : pairedDevices) {
			BluetoothClass bluetoothClass = device.getBluetoothClass();
               if (bluetoothClass != null) {
               	int deviceClass = bluetoothClass.getDeviceClass();
               	if(bluetoothClass.hasService(Service.RENDER) ||
               		deviceClass == Device.AUDIO_VIDEO_WEARABLE_HEADSET ||
               		deviceClass == Device.AUDIO_VIDEO_CAR_AUDIO ||
               		deviceClass == Device.AUDIO_VIDEO_HANDSFREE ) {
                    	//And if any can be used as a audio handset
                    	hasConnectedDevice = true;
                    	break;
               	}
			}
		}
	}
	boolean retVal = hasConnectedDevice && audioManager.isBluetoothScoAvailableOffCall();
	Log.d(THIS_FILE, "Can I do BT ? "+retVal);
	return retVal;
}
 
Example 12
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);

    View view = inflater.inflate(R.layout.view_add_bluetooth_server, container);

    ListView listViewBtPairedPCs = (ListView) view.findViewById(R.id.listViewBtPairedPCs);

    ArrayList<BluetoothDevice> pairedBluetoothList = new ArrayList<>();

    BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> pairedDevices = bluetoothAdapter.getBondedDevices();

    if (pairedDevices.size() > 0) {
        for (BluetoothDevice device : pairedDevices) {
            if(device.getBluetoothClass().getMajorDeviceClass() == BluetoothClass.Device.Major.COMPUTER) {
                pairedBluetoothList.add(device);
            }
        }
        if (pairedBluetoothList.size() == 0) {
            Toast.makeText(getActivity().getApplicationContext(), R.string.bluetooth_no_paired_found, Toast.LENGTH_LONG).show();
            return null;
        }
    } else {
        Toast.makeText(getActivity().getApplicationContext(), R.string.bluetooth_no_paired_found, Toast.LENGTH_LONG).show();
        return null;
    }
    BluetoothPairedDevicesAdapter adapter = new BluetoothPairedDevicesAdapter(getActivity(), pairedBluetoothList, this);
    listViewBtPairedPCs.setAdapter(adapter);
    return view;
}
 
Example 13
Source Project: BLE   Source File: BluetoothClassResolver.java    License: Apache License 2.0 5 votes vote down vote up
public static String resolveMajorDeviceClass(final int majorBtClass) {
    switch (majorBtClass) {
        case BluetoothClass.Device.Major.AUDIO_VIDEO:
            return "Audio/ Video";
        case BluetoothClass.Device.Major.COMPUTER:
            return "Computer";
        case BluetoothClass.Device.Major.HEALTH:
            return "Health";
        case BluetoothClass.Device.Major.IMAGING:
            return "Imaging";
        case BluetoothClass.Device.Major.MISC:
            return "Misc";
        case BluetoothClass.Device.Major.NETWORKING:
            return "Networking";
        case BluetoothClass.Device.Major.PERIPHERAL:
            return "Peripheral";
        case BluetoothClass.Device.Major.PHONE:
            return "Phone";
        case BluetoothClass.Device.Major.TOY:
            return "Toy";
        case BluetoothClass.Device.Major.UNCATEGORIZED:
            return "Uncategorized";
        case BluetoothClass.Device.Major.WEARABLE:
            return "Wearable";
        default:
            return "Unknown (" + majorBtClass + ")";
    }
}
 
Example 14
Source Project: libcommon   Source File: BluetoothDeviceInfo.java    License: Apache License 2.0 5 votes vote down vote up
BluetoothDeviceInfo(final BluetoothDevice device) {
	name = device.getName();
	address =  device.getAddress();
	if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
		type = device.getType();
	} else {
		type = 0;
	}
	final BluetoothClass clazz = device.getBluetoothClass();
	deviceClass = clazz != null ? clazz.getDeviceClass() : 0;
	bondState = device.getBondState();
}
 
Example 15
Source Project: android   Source File: ScanActivity.java    License: MIT License 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();
    if (BluetoothDevice.ACTION_FOUND.equals(action)) {
        BluetoothClass clazz = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
        BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        mDataList.add(device);
        mAdapter.notifyDataSetChanged();
    } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
        System.out.println(">>>>>>change");
    } else if (BluetoothDevice.ACTION_PAIRING_REQUEST.equals(action)) {
        System.out.println(">>>>>>pairing");
    }
}
 
Example 16
Source Project: AndroidBleManager   Source File: BluetoothClassResolver.java    License: Apache License 2.0 5 votes vote down vote up
public static String resolveMajorDeviceClass(final int majorBtClass) {
    switch (majorBtClass) {
        case BluetoothClass.Device.Major.AUDIO_VIDEO:
            return "Audio/ Video";
        case BluetoothClass.Device.Major.COMPUTER:
            return "Computer";
        case BluetoothClass.Device.Major.HEALTH:
            return "Health";
        case BluetoothClass.Device.Major.IMAGING:
            return "Imaging";
        case BluetoothClass.Device.Major.MISC:
            return "Misc";
        case BluetoothClass.Device.Major.NETWORKING:
            return "Networking";
        case BluetoothClass.Device.Major.PERIPHERAL:
            return "Peripheral";
        case BluetoothClass.Device.Major.PHONE:
            return "Phone";
        case BluetoothClass.Device.Major.TOY:
            return "Toy";
        case BluetoothClass.Device.Major.UNCATEGORIZED:
            return "Uncategorized";
        case BluetoothClass.Device.Major.WEARABLE:
            return "Wearable";
        default:
            return "Unknown (" +majorBtClass+ ")";
    }
}
 
Example 17
Source Project: NovarumBluetooth   Source File: NovarumbluetoothModule.java    License: MIT License 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) 
{
    Message msg = Message.obtain();
    String action = intent.getAction();
    
    if(BluetoothDevice.ACTION_FOUND.equals(action))
    {

    	bluetoothDevice        = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
    	BluetoothClass blclass = intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
    	
    	int Majorclass = blclass.getMajorDeviceClass();
    	int minorclass = blclass.getDeviceClass();
    	
    	
        devicelist = new KrollDict();
        
        try
        {
        	devicelist.put("name",bluetoothDevice.getName());
        	devicelist.put("macaddress",bluetoothDevice.getAddress());
        	devicelist.put("pairedstatus",bluetoothDevice.getBondState());
        }
        catch (Exception e) 
        {
            Log.w(TAG, "devicesFound exception: "+e.getMessage());
            postError(e.getMessage());
        }

        devicesFound();
        
    }           
}
 
Example 18
Source Project: assertj-android   Source File: BluetoothClassAssert.java    License: Apache License 2.0 5 votes vote down vote up
public static String majorDeviceClassToString(int majorDeviceClass) {
  return buildNamedValueString(majorDeviceClass) //
      .value(BluetoothClass.Device.Major.AUDIO_VIDEO, "audio_video")
      .value(BluetoothClass.Device.Major.COMPUTER, "computer")
      .value(BluetoothClass.Device.Major.HEALTH, "health")
      .value(BluetoothClass.Device.Major.IMAGING, "imaging")
      .value(BluetoothClass.Device.Major.MISC, "misc")
      .value(BluetoothClass.Device.Major.NETWORKING, "networking")
      .value(BluetoothClass.Device.Major.PERIPHERAL, "peripheral")
      .value(BluetoothClass.Device.Major.PHONE, "phone")
      .value(BluetoothClass.Device.Major.TOY, "toy")
      .value(BluetoothClass.Device.Major.UNCATEGORIZED, "uncategorized")
      .value(BluetoothClass.Device.Major.WEARABLE, "wearable")
      .get();
}
 
Example 19
Source Project: assertj-android   Source File: BluetoothClassAssert.java    License: Apache License 2.0 5 votes vote down vote up
public static String serviceToString(int service) {
  return buildNamedValueString(service) //
      .value(BluetoothClass.Service.AUDIO, "audio")
      .value(BluetoothClass.Service.CAPTURE, "capture")
      .value(BluetoothClass.Service.INFORMATION, "information")
      .value(BluetoothClass.Service.LIMITED_DISCOVERABILITY, "limited_discoverability")
      .value(BluetoothClass.Service.NETWORKING, "networking")
      .value(BluetoothClass.Service.OBJECT_TRANSFER, "object_transfer")
      .value(BluetoothClass.Service.POSITIONING, "positioning")
      .value(BluetoothClass.Service.RENDER, "render")
      .value(BluetoothClass.Service.TELEPHONY, "telephony")
      .get();
}
 
Example 20
Source Project: J2ME-Loader   Source File: DeviceClass.java    License: Apache License 2.0 4 votes vote down vote up
DeviceClass() {
	this(BluetoothClass.Device.PHONE_CELLULAR | BluetoothClass.Service.TELEPHONY);
}
 
Example 21
Source Project: talkback   Source File: BluetoothDeviceListAdapter.java    License: Apache License 2.0 4 votes vote down vote up
@RequiresPermission(allOf = {permission.BLUETOOTH_ADMIN, permission.BLUETOOTH})
private void setDeviceIconResource(ComparableBluetoothDevice bluetoothDevice) {
  int imageResource;
  String description;
  /* Some bluetooth classes used to determine icon are inaccessible from outside of Android.
   * Because of this, the icons displayed here may not match the Android settings page exactly.
   */
  BluetoothClass bluetoothClass = bluetoothDevice.getBluetoothClass();
  /* Show the default Bluetooth icon if the Bluetooth class is null. */
  int majorDeviceClass =
      (bluetoothClass == null) ? Major.MISC : bluetoothClass.getMajorDeviceClass();
  switch (majorDeviceClass) {
      case Major.COMPUTER:
        imageResource = R.drawable.ic_bluetooth_computer;
        description = descriptionManager.computerContentDescription();
        break;
      case Major.PHONE:
        imageResource = R.drawable.ic_bluetooth_phone;
        description = descriptionManager.phoneContentDescription();
        break;
      case Major.PERIPHERAL:
        imageResource = R.drawable.ic_bluetooth_peripheral;
        description = descriptionManager.peripheralContentDescription();
        break;
      case Major.IMAGING:
        imageResource = R.drawable.ic_bluetooth_imaging;
        description = descriptionManager.imagingContentDescription();
        break;
      case Major.AUDIO_VIDEO:
      // We can't get into this case if bluetoothClass is null because majorDeviceClass would
      // be Major.MISC not Major.AUDIO_VIDEO. deviceClass is a separate statement to allow
      // the warning suppression here rather than on the whole method.
      @SuppressWarnings("nullness:dereference.of.nullable")
      int deviceClass = bluetoothClass.getDeviceClass();
      if (deviceClass == Device.AUDIO_VIDEO_HEADPHONES) {
          imageResource = R.drawable.ic_bluetooth_headphone;
          description = descriptionManager.headphoneContentDescription();
          break;
        }
        // Fall-through
      default:
        imageResource = R.drawable.ic_bluetooth_default;
        description = descriptionManager.defaultBluetoothDeviceContentDescription();
    }

    iconView.setImageResource(imageResource);
    iconView.setContentDescription(description);
}
 
Example 22
Source Project: talkback   Source File: ComparableBluetoothDevice.java    License: Apache License 2.0 4 votes vote down vote up
/** Returns the {@link BluetoothClass} of the associated Bluetooth device. */
@Nullable
public BluetoothClass getBluetoothClass() {
  return bluetoothClass;
}
 
Example 23
Source Project: talkback   Source File: ComparableBluetoothDevice.java    License: Apache License 2.0 4 votes vote down vote up
/** Updates the {@link BluetoothClass} associated with this ComparableBluetoothDevice. */
public void setBluetoothClass(@Nullable BluetoothClass bluetoothClass) {
  this.bluetoothClass = bluetoothClass;
}
 
Example 24
Source Project: talkback   Source File: BluetoothEventManager.java    License: Apache License 2.0 4 votes vote down vote up
@RequiresPermission(allOf = {permission.BLUETOOTH, permission.BLUETOOTH_ADMIN})
@Override
public void onReceive(Context context, Intent intent) {
  String action = intent.getAction();
  BluetoothDevice bluetoothDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
  if (bluetoothDevice == null) {
    return;
  }
  ComparableBluetoothDevice comparableBluetoothDevice =
      new ComparableBluetoothDevice(
          context, bluetoothAdapter, bluetoothDevice, bluetoothDeviceActionListener);
  if (BluetoothDevice.ACTION_NAME_CHANGED.equals(action)
      || BluetoothDevice.ACTION_FOUND.equals(action)) {
    String bluetoothDeviceName = intent.getStringExtra(BluetoothDevice.EXTRA_NAME);
    comparableBluetoothDevice.setName(bluetoothDeviceName);
    if (action.equals(BluetoothDevice.ACTION_FOUND)) {
      BluetoothClass bluetoothDeviceClass =
          intent.getParcelableExtra(BluetoothDevice.EXTRA_CLASS);
      comparableBluetoothDevice.setBluetoothClass(bluetoothDeviceClass);
    }

    /* Don't add a device if it's already been bonded (paired) to the device. */
    if (bluetoothDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
      short rssi = intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
      comparableBluetoothDevice.setRssi(rssi);
      dispatchDeviceDiscoveredEvent(comparableBluetoothDevice);
      // TODO: Remove available devices from adapter if they become
      // unavailable. This will most likely be unable to be addressed without API changes.
    } else {
      dispatchDevicePairedEvent(comparableBluetoothDevice);
    }
  } else if (BluetoothDevice.ACTION_ACL_CONNECTED.equals(action)) {
    if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
      comparableBluetoothDevice.setConnectionState(BluetoothConnectionState.CONNECTED);
      dispatchDeviceConnectionStateChangedEvent(comparableBluetoothDevice);
    }
  } else if (BluetoothDevice.ACTION_ACL_DISCONNECTED.equals(action)) {
    if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
      comparableBluetoothDevice.setConnectionState(BluetoothConnectionState.UNKNOWN);
      dispatchDeviceConnectionStateChangedEvent(comparableBluetoothDevice);
    }
  } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
    if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_BONDED) {
      comparableBluetoothDevice.setConnectionState(BluetoothConnectionState.CONNECTED);
      dispatchDevicePairedEvent(comparableBluetoothDevice);
    } else if (bluetoothDevice.getBondState() == BluetoothDevice.BOND_NONE) {
      /* The call to #createBond has completed, but the Bluetooth device isn't bonded, so
       * set the connection state to unavailable. */
      comparableBluetoothDevice.setConnectionState(BluetoothConnectionState.UNAVAILABLE);
      dispatchDeviceConnectionStateChangedEvent(comparableBluetoothDevice);
    }

    /* If we canceled discovery before beginning the pairing process, resume discovery after
     * {@link BluetoothDevice#createBond} finishes. */
    if (bluetoothDevice.getBondState() != BluetoothDevice.BOND_BONDING
        && !bluetoothAdapter.isDiscovering()) {
      bluetoothAdapter.startDiscovery();
    }
  }
}
 
Example 25
@SuppressWarnings({"deprecation", "synthetic-access"})
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (action.equals(BluetoothDevice.ACTION_ACL_CONNECTED)) {
        BluetoothDevice mConnectedHeadset = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
        BluetoothClass bluetoothClass = mConnectedHeadset.getBluetoothClass();
        if (bluetoothClass != null) {
            // Check if device is a headset. Besides the 2 below, are there other
            // device classes also qualified as headset?
            int deviceClass = bluetoothClass.getDeviceClass();
            if (deviceClass == BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE
                    || deviceClass == BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET) {
                // start bluetooth Sco audio connection.
                // Calling startBluetoothSco() always returns faIL here,
                // that why a count down timer is implemented to call
                // startBluetoothSco() in the onTick.
                mAudioManager.setMode(AudioManager.MODE_IN_COMMUNICATION);
                mIsCountDownOn = true;
                mCountDown.start();

                // override this if you want to do other thing when the device is connected.
                onHeadsetConnected();
            }
        }

        Log.d(TAG, mConnectedHeadset.getName() + " connected");
    } else if (action.equals(BluetoothDevice.ACTION_ACL_DISCONNECTED)) {
        Log.d(TAG, "Headset disconnected");

        if (mIsCountDownOn) {
            mIsCountDownOn = false;
            mCountDown.cancel();
        }

        mAudioManager.setMode(AudioManager.MODE_NORMAL);

        // override this if you want to do other thing when the device is disconnected.
        onHeadsetDisconnected();
    } else if (action.equals(AudioManager.ACTION_SCO_AUDIO_STATE_CHANGED)) {
        int state = intent.getIntExtra(AudioManager.EXTRA_SCO_AUDIO_STATE,
                AudioManager.SCO_AUDIO_STATE_ERROR);

        if (state == AudioManager.SCO_AUDIO_STATE_CONNECTED) {
            mIsOnHeadsetSco = true;

            if (mIsStarting) {
                // When the device is connected before the application starts,
                // ACTION_ACL_CONNECTED will not be received, so call onHeadsetConnected here
                mIsStarting = false;
                onHeadsetConnected();
            }

            if (mIsCountDownOn) {
                mIsCountDownOn = false;
                mCountDown.cancel();
            }

            // override this if you want to do other thing when Sco audio is connected.
            onScoAudioConnected();

            Log.d(TAG, "Sco connected");
        } else if (state == AudioManager.SCO_AUDIO_STATE_DISCONNECTED) {
            Log.d(TAG, "Sco disconnected");

            // Always receive SCO_AUDIO_STATE_DISCONNECTED on call to startBluetooth()
            // which at that stage we do not want to do anything. Thus the if condition.
            if (!mIsStarting) {
                mIsOnHeadsetSco = false;

                // Need to call stopBluetoothSco(), otherwise startBluetoothSco()
                // will not be successful.
                mAudioManager.stopBluetoothSco();

                // override this if you want to do other thing when Sco audio is disconnected.
                onScoAudioDisconnected();
            }
        }
    }
}
 
Example 26
Source Project: BluetoothHidEmu   Source File: BluetoothDeviceView.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Checks wether the specified device is a PS3 host. Weirdly enough, the PS3 reports 
 * a device class of 0x2c0108:
 *
 * Which means:
 *    - service (0x2c): Rendering | Capturing | Audio
 *    - major # (0x01): Computer
 *    - minor # (0x08): Server-class computer 
 * 
 * This setup seems rather generic for the ps3, however it will have to do for now. 
 * Another option, would be to check the bluetooth MAC prefix, which identifies the
 * adapter vendor. CECHGxx units seems to be built with Alps Co. adapters (00:1B:FB)
 * But to check it now could refrain this to work on other unchecked units.
 * 
 * So, currently we will only check for service/major/minor.
 * 
 * @param device
 * @return
 */
public static boolean isBluetoothDevicePs3(BluetoothDevice device) {
	
	if (device == null) {
		return false;
	}
	
	BluetoothClass bluetoothClass = device.getBluetoothClass();
	
	boolean isPs3 = PS3_MAJOR_MINOR == bluetoothClass.getDeviceClass();
	
	int i = 0;
	while (isPs3 && i < PS3_SVC_LIST.length) {
		isPs3 = isPs3 && bluetoothClass.hasService(PS3_SVC_LIST[i]);
		i++;
	}
	
	return isPs3;
}
 
Example 27
Source Project: assertj-android   Source File: BluetoothClassAssert.java    License: Apache License 2.0 4 votes vote down vote up
public BluetoothClassAssert(BluetoothClass actual) {
  super(actual, BluetoothClassAssert.class);
}
 
Example 28
Source Project: assertj-android   Source File: BluetoothClassAssert.java    License: Apache License 2.0 4 votes vote down vote up
public static String deviceClassToString(int deviceClass) {
  return buildNamedValueString(deviceClass) //
      .value(BluetoothClass.Device.AUDIO_VIDEO_CAMCORDER, "AUDIO_VIDEO_CAMCORDER")
      .value(BluetoothClass.Device.AUDIO_VIDEO_CAR_AUDIO, "AUDIO_VIDEO_CAR_AUDIO")
      .value(BluetoothClass.Device.AUDIO_VIDEO_HANDSFREE, "AUDIO_VIDEO_HANDSFREE")
      .value(BluetoothClass.Device.AUDIO_VIDEO_HEADPHONES, "AUDIO_VIDEO_HEADPHONES")
      .value(BluetoothClass.Device.AUDIO_VIDEO_HIFI_AUDIO, "AUDIO_VIDEO_HIFI_AUDIO")
      .value(BluetoothClass.Device.AUDIO_VIDEO_LOUDSPEAKER, "AUDIO_VIDEO_LOUDSPEAKER")
      .value(BluetoothClass.Device.AUDIO_VIDEO_MICROPHONE, "AUDIO_VIDEO_MICROPHONE")
      .value(BluetoothClass.Device.AUDIO_VIDEO_PORTABLE_AUDIO, "AUDIO_VIDEO_PORTABLE_AUDIO")
      .value(BluetoothClass.Device.AUDIO_VIDEO_SET_TOP_BOX, "AUDIO_VIDEO_SET_TOP_BOX")
      .value(BluetoothClass.Device.AUDIO_VIDEO_UNCATEGORIZED, "AUDIO_VIDEO_UNCATEGORIZED")
      .value(BluetoothClass.Device.AUDIO_VIDEO_VCR, "AUDIO_VIDEO_VCR")
      .value(BluetoothClass.Device.AUDIO_VIDEO_VIDEO_CAMERA, "AUDIO_VIDEO_VIDEO_CAMERA")
      .value(BluetoothClass.Device.AUDIO_VIDEO_VIDEO_CONFERENCING,
          "AUDIO_VIDEO_VIDEO_CONFERENCING")
      .value(BluetoothClass.Device.AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER,
          "AUDIO_VIDEO_VIDEO_DISPLAY_AND_LOUDSPEAKER")
      .value(BluetoothClass.Device.AUDIO_VIDEO_VIDEO_GAMING_TOY, "AUDIO_VIDEO_VIDEO_GAMING_TOY")
      .value(BluetoothClass.Device.AUDIO_VIDEO_VIDEO_MONITOR, "AUDIO_VIDEO_VIDEO_MONITOR")
      .value(BluetoothClass.Device.AUDIO_VIDEO_WEARABLE_HEADSET, "AUDIO_VIDEO_WEARABLE_HEADSET")
      .value(BluetoothClass.Device.COMPUTER_DESKTOP, "COMPUTER_DESKTOP")
      .value(BluetoothClass.Device.COMPUTER_HANDHELD_PC_PDA, "COMPUTER_HANDHELD_PC_PDA")
      .value(BluetoothClass.Device.COMPUTER_LAPTOP, "COMPUTER_LAPTOP")
      .value(BluetoothClass.Device.COMPUTER_PALM_SIZE_PC_PDA, "COMPUTER_PALM_SIZE_PC_PDA")
      .value(BluetoothClass.Device.COMPUTER_SERVER, "COMPUTER_SERVER")
      .value(BluetoothClass.Device.COMPUTER_UNCATEGORIZED, "COMPUTER_UNCATEGORIZED")
      .value(BluetoothClass.Device.COMPUTER_WEARABLE, "COMPUTER_WEARABLE")
      .value(BluetoothClass.Device.HEALTH_BLOOD_PRESSURE, "HEALTH_BLOOD_PRESSURE")
      .value(BluetoothClass.Device.HEALTH_DATA_DISPLAY, "HEALTH_DATA_DISPLAY")
      .value(BluetoothClass.Device.HEALTH_GLUCOSE, "HEALTH_GLUCOSE")
      .value(BluetoothClass.Device.HEALTH_PULSE_OXIMETER, "HEALTH_PULSE_OXIMETER")
      .value(BluetoothClass.Device.HEALTH_PULSE_RATE, "HEALTH_PULSE_RATE")
      .value(BluetoothClass.Device.HEALTH_THERMOMETER, "HEALTH_THERMOMETER")
      .value(BluetoothClass.Device.HEALTH_UNCATEGORIZED, "HEALTH_UNCATEGORIZED")
      .value(BluetoothClass.Device.HEALTH_WEIGHING, "HEALTH_WEIGHING")
      .value(BluetoothClass.Device.PHONE_CELLULAR, "PHONE_CELLULAR")
      .value(BluetoothClass.Device.PHONE_CORDLESS, "PHONE_CORDLESS")
      .value(BluetoothClass.Device.PHONE_ISDN, "PHONE_ISDN")
      .value(BluetoothClass.Device.PHONE_MODEM_OR_GATEWAY, "PHONE_MODEM_OR_GATEWAY")
      .value(BluetoothClass.Device.PHONE_SMART, "PHONE_SMART")
      .value(BluetoothClass.Device.PHONE_UNCATEGORIZED, "PHONE_UNCATEGORIZED")
      .value(BluetoothClass.Device.TOY_CONTROLLER, "TOY_CONTROLLER")
      .value(BluetoothClass.Device.TOY_DOLL_ACTION_FIGURE, "TOY_DOLL_ACTION_FIGURE")
      .value(BluetoothClass.Device.TOY_GAME, "TOY_GAME")
      .value(BluetoothClass.Device.TOY_ROBOT, "TOY_ROBOT")
      .value(BluetoothClass.Device.TOY_UNCATEGORIZED, "TOY_UNCATEGORIZED")
      .value(BluetoothClass.Device.TOY_VEHICLE, "TOY_VEHICLE")
      .value(BluetoothClass.Device.WEARABLE_GLASSES, "WEARABLE_GLASSES")
      .value(BluetoothClass.Device.WEARABLE_HELMET, "WEARABLE_HELMET")
      .value(BluetoothClass.Device.WEARABLE_JACKET, "WEARABLE_JACKET")
      .value(BluetoothClass.Device.WEARABLE_PAGER, "WEARABLE_PAGER")
      .value(BluetoothClass.Device.WEARABLE_UNCATEGORIZED, "WEARABLE_UNCATEGORIZED")
      .value(BluetoothClass.Device.WEARABLE_WRIST_WATCH, "WEARABLE_WRIST_WATCH")
      .get();
}