14

BluetoothDevice.getName() が null を返すことがあります。どうすれば修正できますか?次のコードでは、remoteDeviceName が null の場合があります。そして、自分のデバイスと他のデバイスを remoteDeviceName で区別する必要があります。

BluetoothAdapter.getDefaultAdapter().startLeScan(new LeScanCallback() {
            @Override
            public void onLeScan(final BluetoothDevice device, final int rssi,
                    byte[] scanRecord) {
                    String remoteDeviceName = device.getName();
                  Log.d("Scanning", "scan device " + remoteDeviceName);
            });
4

7 に答える 7

16

最後に、解決策を見つけました:

1.接続されているデバイスの場合:

サービスorg.bluetooth.service.generic_accessの gatt 特性org.bluetooth.characteristic.gap.device_nameからデバイス名を読み取ります。

2.デバイスが接続されていない場合:

    /**
     * Get device name from ble advertised data
     */
    private LeScanCallback mScanCb = new LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device, final int rssi,
            byte[] scanRecord) {
            final BleAdvertisedData badata = BleUtil.parseAdertisedData(scanRecord);
            String deviceName = device.getName();
            if( deviceName == null ){
                deviceName = badata.getName();
            }
    }


////////////////////// Helper Classes: BleUtil and BleAdvertisedData ///////////////
    final public class BleUtil {        
        private final static String TAG=BleUtil.class.getSimpleName();
        public static BleAdvertisedData parseAdertisedData(byte[] advertisedData) {      
            List<UUID> uuids = new ArrayList<UUID>();
            String name = null;
            if( advertisedData == null ){
                return new BleAdvertisedData(uuids, name);
            }

            ByteBuffer buffer = ByteBuffer.wrap(advertisedData).order(ByteOrder.LITTLE_ENDIAN);
            while (buffer.remaining() > 2) {
                byte length = buffer.get();
                if (length == 0) break;

                byte type = buffer.get();
                switch (type) {
                    case 0x02: // Partial list of 16-bit UUIDs
                    case 0x03: // Complete list of 16-bit UUIDs
                        while (length >= 2) {
                            uuids.add(UUID.fromString(String.format(
                                    "%08x-0000-1000-8000-00805f9b34fb", buffer.getShort())));
                            length -= 2;
                        }
                        break;
                    case 0x06: // Partial list of 128-bit UUIDs
                    case 0x07: // Complete list of 128-bit UUIDs
                        while (length >= 16) {
                            long lsb = buffer.getLong();
                            long msb = buffer.getLong();
                            uuids.add(new UUID(msb, lsb));
                            length -= 16;
                         }
                        break;
                    case 0x09:
                        byte[] nameBytes = new byte[length-1];
                        buffer.get(nameBytes);
                        try {
                            name = new String(nameBytes, "utf-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printStackTrace();
                        }
                        break;
                    default:
                        buffer.position(buffer.position() + length - 1);
                        break;
                    }
                }
            return new BleAdvertisedData(uuids, name);
        }
    }


    public class BleAdvertisedData {
        private List<UUID> mUuids;
        private String mName;
        public BleAdvertisedData(List<UUID> uuids, String name){
            mUuids = uuids;
            mName = name;
        }

        public List<UUID> getUuids(){
            return mUuids;
        }

        public String getName(){
            return mName;
        }
    }
于 2014-10-14T13:09:38.663 に答える
7

BluetoothDevice.getName()null名前を特定できなかった場合に返されることがあります。これには、さまざまな要因が考えられます。とにかく、名前はデバイスのフレンドリ名であり、他のデバイスと区別するために使用しないでください。代わりに、ハードウェア アドレスを使用してgetAddress()ください。

于 2014-10-10T03:09:26.670 に答える
0

誰かが解決策を見つけていないからです。

  1. Bluetooth 広告パッケージの最大サイズは 31 バイトです。したがって、デバイスの名前が長い場合。切り捨てることができます。参照: https://devzone.nordicsemi.com/f/nordic-qa/14/what-s-the-maximum-size-for-an-advertisement-package

  2. Bluetooth デバイスの正しい名前 (長い名前であっても) を取得したい場合は、使用しないでくださいstartLeScan()。その代わりに、 method を使用しstartDiscovery()ます。Google は次のように述べています。「検出プロセスには、通常、約 12 秒間の照会スキャンと、検出された各デバイスのページ スキャンが含まれ、その Bluetooth 名を取得します。」私は試してみましたが、それは魅力のように機能します。参照: https://developer.android.com/guide/topics/connectivity/bluetooth

于 2020-06-13T04:54:01.723 に答える