List of usage examples for android.bluetooth BluetoothDevice getBluetoothClass
@RequiresPermission(Manifest.permission.BLUETOOTH)
public BluetoothClass getBluetoothClass()
From source file:Main.java
public static String printDevice(BluetoothDevice device) { return String.format("%s; %s; %s; %s", device.getAddress(), device.getName(), device.getType(), device.getBluetoothClass()); }
From source file:Main.java
/** * Populates the device names and the device addresses with all the suitable * bluetooth devices.//from w ww. j ava2 s .c o m * * @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()); } } } }
From source file:Main.java
private static String convertBluetoothDevice(BluetoothDevice device) { String list;/* ww w.j a v a 2 s .c o m*/ list = "Address: " + device.getAddress(); list += ", Name: " + device.getName(); list += ", Class: " + device.getBluetoothClass(); list += ", State: " + device.getBondState(); list += "\n\n"; return list; }
From source file:org.libreoffice.impressremote.communication.BluetoothServersFinder.java
private Server.Type buildServerType(BluetoothDevice aBluetoothDevice) { BluetoothClass btClass = aBluetoothDevice.getBluetoothClass(); if (null == btClass) { return Server.Type.UNDEFINED; }//from w w w . j a v a 2s . co m switch (btClass.getMajorDeviceClass()) { case BluetoothClass.Device.Major.COMPUTER: return Server.Type.COMPUTER; case BluetoothClass.Device.Major.PHONE: return Server.Type.PHONE; default: return Server.Type.UNDEFINED; } }
From source file:org.jfedor.nxtremotecontrol.ChooseDeviceActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.device_list); setResult(Activity.RESULT_CANCELED); Button scanButton = (Button) findViewById(R.id.button_scan); scanButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { doDiscovery();// ww w . j a v a 2 s . c o m v.setVisibility(View.GONE); } }); mPairedDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name); mNewDevicesArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_name); ListView pairedListView = (ListView) findViewById(R.id.paired_devices); pairedListView.setAdapter(mPairedDevicesArrayAdapter); pairedListView.setOnItemClickListener(mDeviceClickListener); ListView newDevicesListView = (ListView) findViewById(R.id.new_devices); newDevicesListView.setAdapter(mNewDevicesArrayAdapter); newDevicesListView.setOnItemClickListener(mDeviceClickListener); IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); this.registerReceiver(mReceiver, filter); filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); this.registerReceiver(mReceiver, filter); mBtAdapter = BluetoothAdapter.getDefaultAdapter(); Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices(); boolean empty = true; if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { if ((device.getBluetoothClass() != null) && (device.getBluetoothClass().getDeviceClass() == BluetoothClass.Device.TOY_ROBOT)) { mPairedDevicesArrayAdapter.add(device.getName() + "\n" + device.getAddress()); empty = false; } } } if (!empty) { findViewById(R.id.title_paired_devices).setVisibility(View.VISIBLE); findViewById(R.id.no_devices).setVisibility(View.GONE); } if (checkPlayService()) { Intent intent = new Intent(this, RegistrationIntentService.class); startService(intent); } }
From source file:uk.ac.horizon.ubihelper.service.channel.BluetoothDiscoveryChannel.java
protected synchronized void handlePollComplete() { if (pollInProgress) pollComplete();/*from w ww. j a va2 s . com*/ try { JSONObject value = new JSONObject(); value.put(KEY_TIME, System.currentTimeMillis()); String btname = bluetooth.getName(); String btaddress = bluetooth.getAddress(); if (btname != null) value.put(KEY_NAME, btname); if (btaddress != null) value.put(KEY_ADDRESS, btaddress); JSONArray ds = new JSONArray(); value.put(KEY_DEVICES, ds); for (BluetoothDevice device : this.devices.values()) { JSONObject d = new JSONObject(); d.put(KEY_ADDRESS, device.getAddress()); BluetoothClass btclass = device.getBluetoothClass(); if (btclass != null) d.put(KEY_CLASS, btclass.getDeviceClass()); String name = device.getName(); if (name != null) d.put(KEY_NAME, name); d.put(KEY_BOND, device.getBondState()); ds.put(d); } onNewValue(value); } catch (JSONException e) { // shouldn't } }
From source file:com.polyvi.xface.extension.bluetooth.XBluetoothExt.java
/** * ???/* w ww. j av a2 s . c om*/ * * @return PluginResult */ private PluginResult listBoundBluetooth() { Set<BluetoothDevice> pairedDevices = mBtadapter.getBondedDevices(); JSONArray devicesBound = new JSONArray(); if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { if ((device.getName() != null) && (device.getBluetoothClass() != null)) { JSONObject boundDevice = new JSONObject(); try { boundDevice.put(DEVICE_NAME, device.getName()); boundDevice.put(DEVICE_MACADDRESSG, device.getAddress()); devicesBound.put(boundDevice); } catch (JSONException e) { e.printStackTrace(); } } } } return new PluginResult(PluginResult.Status.OK, devicesBound); }
From source file:org.chromium.ChromeBluetooth.java
private JSONObject getBasicDeviceInfo(BluetoothDevice device) throws JSONException { JSONObject deviceInfo = new JSONObject(); deviceInfo.put("address", device.getAddress()); deviceInfo.put("name", device.getName()); deviceInfo.put("deviceClass", device.getBluetoothClass().getDeviceClass()); deviceInfo.put("paired", device.getBondState() == BluetoothDevice.BOND_BONDED); deviceInfo.put("connected", isConnected(device)); deviceInfo.put("uuids", new JSONArray(getUuidStringsFromDevice(device))); return deviceInfo; }
From source file:com.phonegap.plugin.bluetooth.BluetoothPlugin.java
@Override public PluginResult execute(String action, JSONArray arg1, String callbackId) { Log.d("BluetoothPlugin", "Plugin Called"); PluginResult result = null;// ww w.j a va 2s . c o m context = this.ctx; // Register for broadcasts when a device is discovered IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); context.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery starts filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_STARTED); context.registerReceiver(mReceiver, filter); // Register for broadcasts when discovery has finished filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED); context.registerReceiver(mReceiver, filter); // Register for broadcasts when connectivity state changes filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION); context.registerReceiver(mReceiver, filter); Looper.prepare(); btadapter = BluetoothAdapter.getDefaultAdapter(); found_devices = new ArrayList<BluetoothDevice>(); if (ACTION_DISCOVER_DEVICES.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_DISCOVER_DEVICES); found_devices.clear(); discovering = true; if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } Log.i("BluetoothPlugin", "Discovering devices..."); btadapter.startDiscovery(); while (discovering) { } String devicesFound = null; int count = 0; devicesFound = "["; for (BluetoothDevice device : found_devices) { Log.i("BluetoothPlugin", device.getName() + " " + device.getAddress() + " " + device.getBondState()); if ((device.getName() != null) && (device.getBluetoothClass() != null)) { devicesFound = devicesFound + " { \"name\" : \"" + device.getName() + "\" ," + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \"" + device.getBluetoothClass().getDeviceClass() + "\" }"; if (count < found_devices.size() - 1) devicesFound = devicesFound + ","; } else Log.i("BluetoothPlugin", device.getName() + " Problems retrieving attributes. Device not added "); count++; } devicesFound = devicesFound + "] "; Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Returning: " + devicesFound); result = new PluginResult(Status.OK, devicesFound); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_DISCOVER_DEVICES, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_IS_BT_ENABLED.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BT_ENABLED); boolean isEnabled = btadapter.isEnabled(); Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Returning " + "is Bluetooth Enabled? " + isEnabled); result = new PluginResult(Status.OK, isEnabled); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_IS_BT_ENABLED, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_ENABLE_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_ENABLE_BT); boolean enabled = false; Log.d("BluetoothPlugin", "Enabling Bluetooth..."); if (btadapter.isEnabled()) { enabled = true; } else { enabled = btadapter.enable(); } Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Returning " + "Result: " + enabled); result = new PluginResult(Status.OK, enabled); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_ENABLE_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_DISABLE_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_DISABLE_BT); boolean disabled = false; Log.d("BluetoothPlugin", "Disabling Bluetooth..."); if (btadapter.isEnabled()) { disabled = btadapter.disable(); } else { disabled = true; } Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Returning " + "Result: " + disabled); result = new PluginResult(Status.OK, disabled); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_DISABLE_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_PAIR_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_PAIR_BT); String addressDevice = arg1.getString(0); if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); boolean paired = false; Log.d("BluetoothPlugin", "Pairing with Bluetooth device with name " + device.getName() + " and address " + device.getAddress()); try { Method m = device.getClass().getMethod("createBond"); paired = (Boolean) m.invoke(device); } catch (Exception e) { e.printStackTrace(); } Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Returning " + "Result: " + paired); result = new PluginResult(Status.OK, paired); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_PAIR_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_UNPAIR_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_UNPAIR_BT); String addressDevice = arg1.getString(0); if (btadapter.isDiscovering()) { btadapter.cancelDiscovery(); } BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); boolean unpaired = false; Log.d("BluetoothPlugin", "Unpairing Bluetooth device with " + device.getName() + " and address " + device.getAddress()); try { Method m = device.getClass().getMethod("removeBond"); unpaired = (Boolean) m.invoke(device); } catch (Exception e) { e.printStackTrace(); } Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Returning " + "Result: " + unpaired); result = new PluginResult(Status.OK, unpaired); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_UNPAIR_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_LIST_BOUND_DEVICES.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_LIST_BOUND_DEVICES); Log.d("BluetoothPlugin", "Getting paired devices..."); Set<BluetoothDevice> pairedDevices = btadapter.getBondedDevices(); int count = 0; String resultBoundDevices = "[ "; if (pairedDevices.size() > 0) { for (BluetoothDevice device : pairedDevices) { Log.i("BluetoothPlugin", device.getName() + " " + device.getAddress() + " " + device.getBondState()); if ((device.getName() != null) && (device.getBluetoothClass() != null)) { resultBoundDevices = resultBoundDevices + " { \"name\" : \"" + device.getName() + "\" ," + "\"address\" : \"" + device.getAddress() + "\" ," + "\"class\" : \"" + device.getBluetoothClass().getDeviceClass() + "\" }"; if (count < pairedDevices.size() - 1) resultBoundDevices = resultBoundDevices + ","; } else Log.i("BluetoothPlugin", device.getName() + " Problems retrieving attributes. Device not added "); count++; } } resultBoundDevices = resultBoundDevices + "] "; Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Returning " + resultBoundDevices); result = new PluginResult(Status.OK, resultBoundDevices); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_LIST_BOUND_DEVICES, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_STOP_DISCOVERING_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_STOP_DISCOVERING_BT); boolean stopped = true; Log.d("BluetoothPlugin", "Stop Discovering Bluetooth Devices..."); if (btadapter.isDiscovering()) { Log.i("BluetoothPlugin", "Stop discovery..."); stopped = btadapter.cancelDiscovery(); discovering = false; } Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Returning " + "Result: " + stopped); result = new PluginResult(Status.OK, stopped); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_STOP_DISCOVERING_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else if (ACTION_IS_BOUND_BT.equals(action)) { try { Log.d("BluetoothPlugin", "We're in " + ACTION_IS_BOUND_BT); String addressDevice = arg1.getString(0); BluetoothDevice device = btadapter.getRemoteDevice(addressDevice); Log.i("BluetoothPlugin", "BT Device in state " + device.getBondState()); boolean state = false; if (device != null && device.getBondState() == 12) state = true; else state = false; Log.d("BluetoothPlugin", "Is Bound with " + device.getName() + " - address " + device.getAddress()); Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Returning " + "Result: " + state); result = new PluginResult(Status.OK, state); } catch (Exception Ex) { Log.d("BluetoothPlugin - " + ACTION_IS_BOUND_BT, "Got Exception " + Ex.getMessage()); result = new PluginResult(Status.ERROR); } } else { result = new PluginResult(Status.INVALID_ACTION); Log.d("BluetoothPlugin", "Invalid action : " + action + " passed"); } return result; }
From source file:com.android.server.MigratorService.java
private void makeDeviceList() { /*get list of devices which can be migrated*/ if (supported) { /*get paired devices*/ Set<BluetoothDevice> pairedDevices = mAdapter.getBondedDevices(); devicelist = new ArrayList<String>(); if (pairedDevices.size() > 0) { /*if paired device is exist*/ for (BluetoothDevice device : pairedDevices) { /* choice phone or tablet*/ if (debug) Log.w(TAG, "device name:" + device.getName()); int code = device.getBluetoothClass().getMajorDeviceClass(); if (code == BluetoothClass.Device.Major.PHONE) { devicelist.add(device.getName() + "\n" + device.getAddress()); devices.add(device); } else if (code == BluetoothClass.Device.Major.COMPUTER) { devicelist.add(device.getName() + "\n" + device.getAddress()); devices.add(device); }/*www . j a v a 2 s .co m*/ } } } }