Object[] devices = (Object []) bondedDevices.toArray(); BluetoothDevice device = (BluetoothDevice) devices[position]; ParcelUuid[] uuids = device.getUuids(); BluetoothSocket socket = device.createRfcommSocketToServiceRecord(uuids[0].getUuid()); socket.connect();
@Test public void getUuids_setUuidsNotCalled_shouldReturnNull() throws Exception { BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS); assertThat(device.getUuids()).isNull(); }
@Test public void canSetAndGetUuids() throws Exception { BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(MOCK_MAC_ADDRESS); ParcelUuid[] uuids = new ParcelUuid[] { ParcelUuid.fromString("00000000-1111-2222-3333-000000000011"), ParcelUuid.fromString("00000000-1111-2222-3333-0000000000aa") }; shadowOf(device).setUuids(uuids); assertThat(device.getUuids()).isEqualTo(uuids); }
BluetoothDevice device; ParcelUuid list[] = device.getUuids(); //use e.g. first list[0]
private void showUUID() { StringBuilder builder = new StringBuilder(); ParcelUuid[] uuids = mDevice.getUuids(); if (null == uuids) { return; } for (int i = 0; i < uuids.length; i++) { ParcelUuid uuid = uuids[i]; builder.append(uuid.getUuid().toString()); } tvContent.setText(builder.toString()); } }
private String showUUID(BluetoothDevice device) { StringBuilder builder = new StringBuilder(); ParcelUuid[] uuids = device.getUuids(); if (null == uuids) { return ""; } for (int i = 0; i < uuids.length; i++) { ParcelUuid uuid = uuids[i]; builder.append(uuid.getUuid().toString()); } return builder.toString(); }
BluetoothDevice mmDevice; boolean temp = mmDevice.fetchUuidsWithSdp(); UUID uuid = null; if( temp ){ uuid = mmDevice.getUuids()[0].getUuid(); } tmp = device.createRfcommSocketToServiceRecord(uuid);
public static boolean isGlass(BluetoothDevice device) { ParcelUuid[] uuids = device.getUuids(); for (ParcelUuid id : uuids) { if (id.equals(ParcelUuid.fromString("f96647cf-7f25-4277-843d-f407b4192f8b"))) { return true; } } return false; }
/** * Log supported UUIDs of specified BluetoothSocket * @param socket Socket to log * @param msg Message to prepend the UUIDs */ private void logSocketUuids(BluetoothSocket socket, String msg) { if(log.isLoggable(Level.INFO)) { StringBuilder message = new StringBuilder(msg); // dump supported UUID's message.append(" - UUIDs:"); ParcelUuid uuids[] = socket.getRemoteDevice().getUuids(); for (ParcelUuid uuid: uuids) { message.append(uuid.getUuid().toString()).append(","); } log.log(Level.FINE, message.toString()); } }
BluetoothDevice device; String deviceName = ... selected or hardcoded device name. See Android HDR sample code BluetoothDevice[] mAllBondedDevices = (BluetoothDevice[]) mBluetoothAdapter.getBondedDevices().toArray(new BluetoothDevice[0]); for (BluetoothDevice d : mAllBondedDevices) { if (deviceName.equals(d.getName())) { device = d; break; } } UUID uuid = device.getUuids()[0].getUuid(); //FAILED: socket = device.createRfcommSocketToServiceRecord(uuid); // Succeeds: Warning, INSECURE! socket = device.createInsecureRfcommSocketToServiceRecord(uuid); socket.connect(); this.dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); this.dis = new DataInputStream(new BufferedInputStream(socket.getInputStream())); //etc
/** * Check if a device supports HID Host profile. * * @param device Device to check. * @return {@code true} if the HID Host profile is supported, {@code false} otherwise. */ public boolean isProfileSupported(BluetoothDevice device) { // If a device reports itself as a HID Device, then it isn't a HID Host. ParcelUuid[] uuidArray = device.getUuids(); if (uuidArray != null) { for (ParcelUuid uuid : uuidArray) { if (HID_UUID.equals(uuid) || HOGP_UUID.equals(uuid)) { return false; } } } return true; }
Set<BluetoothDevice> devices = BluetoothAdapter.getDefaultAdapter().getBondedDevices(); BluetoothDevice glass = null; for (BluetoothDevice d : devices { ParcelUuid[] uuids = d.getUuids(); for (ParcelUuid p : uuids) { System.out.println(p.getUuid().toString()); if (p.getUuid().toString().equals("fafbdd20-83f0-4389-addf-917ac9dae5b1")) { //found in the GlassHome.apk for XE16. glass = d; break; } //NOTE: you may need to adjust this UIUD. } if (glass != null) { break; } } if (glass != null) { if (glass.getBondState().equals(BOND_BONDED) { //This last check makes sure you are through the pairing process. } }
ParcelUuid[] uuids = device.getUuids(); if (null != uuids && uuids.length > 0) { int size = uuids.length;
@Override public boolean workerBefore() { try { if (null == mSocket) { UUID uuid = DEFAULT_UUID; ParcelUuid[] uuids = mDevice.getUuids(); if (null != uuids && uuids.length > 0) { uuid = uuids[0].getUuid(); } mSocket = mDevice.createRfcommSocketToServiceRecord(uuid); } mSocket.connect(); // 阻塞的 mIn = mSocket.getInputStream(); } catch (IOException e) { e.printStackTrace(); return false; } tempBuffer = new byte[SIZE]; readBuffer = new byte[SIZE]; return super.workerBefore(); }
if(dev.fetchUuidsWithSdp()) Parcelable a[]=dev.getUuids(); Toast.makeText(getApplicationContext(),dev.getName()+":"+a[0],Toast.LENGTH_SHORT).show();
@Override public void run() { try { boolean gotuuid = btDevices.getItem(position) .fetchUuidsWithSdp(); UUID uuid = btDevices.getItem(position).getUuids()[0] .getUuid(); mbtSocket = btDevices.getItem(position) .createRfcommSocketToServiceRecord(uuid); mbtSocket.connect(); } catch (IOException ex) { runOnUiThread(socketErrorRunnable); try { mbtSocket.close(); } catch (IOException e) { e.printStackTrace(); } mbtSocket = null; } finally { runOnUiThread(new Runnable() { @Override public void run() { finish(); } }); } } });