get Device Information Text from Bluetooth - Android Bluetooth

Android examples for Bluetooth:Bluetooth Device

Description

get Device Information Text from Bluetooth

Demo Code


import android.bluetooth.BluetoothDevice;

public class Main {
  public static String device_name = "";

  public static String getDeviceInfoText(BluetoothDevice device, int rssi, byte[] scanRecord) {
    device_name = device.getName();//w  w w . j  a  v  a2 s  .c o  m
    return new StringBuilder().append("Name: ").append(device.getName()).append("\nMAC: ").append(device.getAddress())
        .append("\nRSSI: ").append(rssi).append("\nScan Record:").append(parseScanRecord(scanRecord)).toString();
  }

  private static String parseScanRecord(byte[] scanRecord) {
    StringBuilder output = new StringBuilder();
    int i = 0;
    while (i < scanRecord.length) {
      int len = scanRecord[i++] & 0xFF;
      if (len == 0)
        break;
      switch (scanRecord[i] & 0xFF) {
      // https://www.bluetooth.org/en-us/specification/assigned-numbers/generic-access-profile
      case 0x0A: // Tx Power
        output.append("\n  Tx Power: ").append(scanRecord[i + 1]);
        break;
      case 0xFF: // Manufacturer Specific data (RFduinoBLE.advertisementData)
        output.append("\n  Advertisement Data: ");
        break;
      }
      i += len;
    }
    return output.toString();
  }
}

Related Tutorials