我刚好做过这方面的研究,直接拿我之前做过的笔记了,欢迎关注、采纳、持续交流Andorid问题
一共有 五大方法:(推荐第五种)
1设备ID(DEVICE_ID)
Android系统为开发者提供的用于标识手机设备的标识码。它会根据不同的手机设备返回IMEI,MEID或者ESN码(IMEI是手机的身份z,MEID是CDMA制式(电信运营的)的专用身份z;IMEI是15位,MEID是14位)。
l 获取方法
TelephonyManager tm = (TelephonyManager) contextgetSystemService(ContextTELEPHONY_SERVICE);String DEVICE_ID =tmgetDeviceId();
l 注意点:
没有通话的硬件功能,无法获得DEVICE_ID。
需要READ_PHONE_STATE权限,部分用户会怀疑软件的安全性。
(Android 60 以上需要用户手动赋予该权限)
厂商定制系统中的Bug,可能是一串0或者一串号。
2 SIM卡序列号
不同SIM卡的序列号不同
l 获取办法
TelephonyManager tm = (TelephonyManager)contextgetSystemService(ContextTELEPHONY_SERVICE);String simSerialNum = tmgetSimSerialNumber();
l 注意点
手机未装SIM卡或者不可用时,返回 null
也需要 READ_PHONE_STATE 权限。
3 设备序列号(SERIAL)
l 获取办法
String serialNum = androidosBuildSERIAL;l 注意点
所有的CDMA 设备对于却返回一个空值
Android系统23版本以上
4 ANDROID_ID
在设备首次启动时,系统会随机生成一个64位的数字,并把这个数字以16进制字符串的形式保存下来,这个16进制的字符串就是ANDROID_ID,当设备被wipe后该值会被重置。
l 获取办法
String ANDROID_ID = SystemgetString(getContentResolver(), SettingsSystemANDROID_ID);l 注意点
定制系统的Bug:不同的设备可能会产生相同的,有些设备返回的值为null。
在 Android 42 及以上, 设备启用多用户功能后,每个用户的 Android_ID 不相同。
部分设备的ANDROID_ID和TelephonyManagergetDeviceId()返回相同的值
5 自定义UUID(本人推荐)
public class Installation {private static String sID = null;
private static final String INSTALLATION = "INSTALLATION";
public synchronized static String id(Context context) {
if (sID == null) {
File installation = new File(contextgetFilesDir(), INSTALLATION);
try {
if (!installationexists()) writeInstallationFile(installation);
sID = readInstallationFile(installation);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return sID;
}
private static String readInstallationFile(File installation) throws IOException {
RandomAccessFile f = new RandomAccessFile(installation, "r");
byte[] bytes = new byte[(int) flength()];
freadFully(bytes);
fclose();
return new String(bytes);
}
private static void writeInstallationFile(File installation) throws IOException {
FileOutputStream out = new FileOutputStream(installation);
String id = UUIDrandomUUID()toString();
outwrite(idgetBytes());
outclose();
}
}
Generic Attribute Profile (GATT)
通过BLE连接,读写属性类小数据的Profile通用规范。现在所有的BLE应用Profile都是基于GATT的。
Attribute Protocol (ATT)
GATT是基于ATT Protocol的。ATT针对BLE设备做了专门的优化,具体就是在传输过程中使用尽量少的数据。每个属性都有一个唯一的UUID,属性将以characteristics and services的形式传输。
Characteristic
Characteristic可以理解为一个数据类型,它包括一个value和0至多个对次value的描述(Descriptor)。
是做安卓开发吗?是接收不到蓝牙数据还是什么情况?先要清楚血压计的数据传输机制,是实时传输还是接收到指令在传输等。
Generic Attribute Profile (GATT)
通过BLE连接,读写属性类小数据的Profile通用规范。现在所有的BLE应用Profile都是基于GATT的。
Attribute Protocol (ATT)
GATT是基于ATT Protocol的。ATT针对BLE设备做了专门的优化,具体就是在传输过程中使用尽量少的数据。每个属性都有一个唯一的UUID,属性将以characteristics and services的形式传输。
Characteristic
Characteristic可以理解为一个数据类型,它包括一个value和0至多个对次value的描述(Descriptor)。
Descriptor
对Characteristic的描述,例如范围、计量单位等。
Service
Characteristic的集合。例如一个service叫做“Heart Rate Monitor”,它可能包含多个Characteristics,其中可能包含一个叫做“heart rate measurement"的Characteristic。
二、角色和职责:
Android设备与BLE设备交互有两组角色:
中心设备和外围设备(Central vs peripheral);
GATT server vs GATT client
Central vs peripheral:
中心设备和外围设备的概念针对的是BLE连接本身。Central角色负责scan advertisement。而peripheral角色负责make advertisement。
GATT server vs GATT client:
这两种角色取决于BLE连接成功后,两个设备间通信的方式。
举例说明:
现 有一个活动追踪的BLE设备和一个支持BLE的Android设备。Android设备支持Central角色,而BLE设备支持peripheral角 色。创建一个BLE连接需要这两个角色都存在,都仅支持Central角色或者都仅支持peripheral角色则无法建立连接。
当 连接建立后,它们之间就需要传输GATT数据。谁做server,谁做client,则取决于具体数据传输的情况。例如,如果活动追踪的BLE设备需要向 Android设备传输sensor数据,则活动追踪器自然成为了server端;而如果活动追踪器需要从Android设备获取更新信息,则 Android设备作为server端可能更合适。
三、权限及feature:
和经典蓝牙一样,应用使用蓝牙,需要声明BLUETOOTH权限,如果需要扫描设备或者 *** 作蓝牙设置,则还需要BLUETOOTH_ADMIN权限:
<uses-permission android:name="androidpermissionBLUETOOTH"/>
<uses-permission android:name="androidpermissionBLUETOOTH_ADMIN"/>
除了蓝牙权限外,如果需要BLE feature则还需要声明uses-feature:
<uses-feature android:name="androidhardwarebluetooth_le" android:required="true"/>
按时required为true时,则应用只能在支持BLE的Android设备上安装运行;required为false时,Android设备均可正常安装运行,需要在代码运行时判断设备是否支持BLE feature:
// Use this check to determine whether BLE is supported on the device Then
// you can selectively disable BLE-related features
if (!getPackageManager()hasSystemFeature(PackageManagerFEATURE_BLUETOOTH_LE)) {
ToastmakeText(this, Rstringble_not_supported, ToastLENGTH_SHORT)show();
finish();
}
四、启动蓝牙:
在使用蓝牙BLE之前,需要确认Android设备是否支持BLE feature(required为false时),另外要需要确认蓝牙是否打开。
如果发现不支持BLE,则不能使用BLE相关的功能。如果支持BLE,但是蓝牙没打开,则需要打开蓝牙。
打开蓝牙的步骤:
1、获取BluetoothAdapter
BluetoothAdapter是Android系统中所有蓝牙 *** 作都需要的,它对应本地Android设备的蓝牙模块,在整个系统中BluetoothAdapter是单例的。当你获取到它的示例之后,就能进行相关的蓝牙 *** 作了。
获取BluetoothAdapter代码示例如下:
// Initializes Bluetooth adapter
final BluetoothManager bluetoothManager =
(BluetoothManager) getSystemService(ContextBLUETOOTH_SERVICE);
mBluetoothAdapter = bluetoothManagergetAdapter();
注:这里通过getSystemService获取BluetoothManager,再通过BluetoothManager获取BluetoothAdapter。BluetoothManager在Android43以上支持(API level 18)。
2、判断是否支持蓝牙,并打开蓝牙
获取到BluetoothAdapter之后,还需要判断是否支持蓝牙,以及蓝牙是否打开。
如果没打开,需要让用户打开蓝牙:
private BluetoothAdapter mBluetoothAdapter;
// Ensures Bluetooth is available on the device and it is enabled If not,
// displays a dialog requesting user permission to enable Bluetooth
if (mBluetoothAdapter == null || !mBluetoothAdapterisEnabled()) {
Intent enableBtIntent = new Intent(BluetoothAdapterACTION_REQUEST_ENABLE);
startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
}
这是Ble极简系列的第二篇文章,上一篇 Android BLE低功耗蓝牙开发极简系列(一)之扫描与连接 主要是扫描连接,这一篇主要是读写 *** 作。
在连接成功后,可以通过Gatt进行discoverServices()。
在mGattCallback 回调添加Servicest的相关回调
当返回的status == BluetoothGattGATT_SUCCESS时,进行读写以及通知相关的 *** 作, 调用writeDescriptor(),注意设置setValue为ENABLE_INDICATION_VALUE,否则可能后续读取不到数据。
设置成功,会在onDescriptorWrite方法进行回调,注意UUID_SERVICE,UUID_NOTIFICATION特征值UUID,可以询问公司固件端的开发人员,和开发人员配合修改。
读取数据在onCharacteristicChanged方法中,注意进制间的转换。
一定要进行读写开关 *** 作,注意descriptorsetValue(BluetoothGattDescriptorENABLE_INDICATION_VALUE),否则可能读取不到数据。
喜欢可以关注博主 BleDemo
以上就是关于如何获取Android唯一标识全部的内容,包括:如何获取Android唯一标识、android 蓝牙4.0ble的uuid怎么修改、android 血压计通过蓝牙连接手机获取数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)