Android 获取Device信息源代码分析

  1. 获取电话信息的代码如下所示:
Build bd = new Build();
        String model = bd.MODEL;
        System.out.println("MODEL= " + model);

        /**
         * getDeviceId
         *getImei
         * getDeviceSvn
         * getSubscriberId
         * getIccSerialNumber
         * getLine1Number
         * getLine1AlphaTag
         * getVoiceMailAlphaTag
         */


        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        StringBuilder sb = new StringBuilder();
        //获取唯一设备ID
        sb.append("\nDeviceId(IMEI) = " + tm.getDeviceId());
        //获取手机方位
//        sb.append("\nPhoneLocation = " + tm.getCellLocation().toString());

        //获取设备的软件版本
        sb.append("\nDeviceSoftwareVersion = " + tm.getDeviceSoftwareVersion());

        //获取电话号码
        sb.append("\nLine1Number = " + tm.getLine1Number());

        //获取ISO标准的国家码
//        sb.append("\nNetworkCountryIso = " + tm.getNetworkCountryIso());

        //(mobile contry code) + (mobile network code)
//        sb.append("\nNetworkOperator = " + tm.getNetworkOperator());


//        sb.append("\nNetworkOperatorName = " + tm.getNetworkOperatorName());

        //当前使用的网络类型
//        sb.append("\nNetworkType = " + tm.getNetworkType());

        //手机类型,如
        /**
         * PHONE_TYPE_NONE 无信号
         * PHONE_TYPE_GSM gsm信号
         * PHONE_TYPE_CDMA CDMA信号
         */
//        sb.append("\nPhoneType = " + tm.getPhoneType());

        //获取ISO国家码
//        sb.append("\nSimCountryIso = " + tm.getSimCountryIso());

        //获取SIM卡提供的移动国家码和移动网络码.56位的十进制数字
//        sb.append("\nSimOperator = " + tm.getSimOperator());

        //服务商名称,例如:中国移动、联通
//        sb.append("\nSimOperatorName = " + tm.getSimOperatorName());

        //SIM卡的序列号
        //需要权限:READ_PHONE_STATE
        sb.append("\nSimSerialNumber = " + tm.getSimSerialNumber());

        //SIM卡的序列号
        sb.append("\nSimState = " + tm.getSimState());

        //唯一的用户ID:
        // 例如:IMSI(国际移动用户识别码) for a GSM phone.
        //需要权限:READ_PHONE_STATE
        sb.append("\nSubscriberId(IMSI) = " + tm.getSubscriberId());

        // 取得和语音邮件相关的标签,即为识别符
        // 需要权限:READ_PHONE_STATE
        sb.append("\nSubscriberId(IMSI) = " + tm.getVoiceMailAlphaTag());
        //获取语音邮件号码:
        //需要权限:READ_PHONE_STATE
//        sb.append("\nVoiceMailNumber = " + tm.getVoiceMailNumber());
        System.out.println(sb.toString());

  1. /home/wdy/Android-5.0/android/packages/services/Telephony/src/com/android/phone/PhoneInterfaceManager.java实现 ITelephony.Stub;

/home/wdy/Android-5.0/android/frameworks/base/telephony/java/android/telephony/TelephonyManager.java
3.

Android平台上,使用BLE(低功耗蓝牙)进行设备通信需要编写源代码。下面是一个简单的示例,展示了如何启用BLE功能、搜索和连接设备、发送和接收数据。 首先,需要确保应用程序在AndroidManifest.xml文件中申请必要的权限和特性。 ```xml <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/> ``` 接下来,在应用程序的MainActivity中创建一个BluetoothAdapter对象,并启用BLE功能。 ```java BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) { // 设备不支持蓝牙或蓝牙未启用 // 进行相关处理 } ``` 然后,需要创建一个BluetoothLeScanner对象来搜索BLE设备。可以通过扫描结果回调来获取设备信息。 ```java BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); bluetoothLeScanner.startScan(new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { // 获取设备信息 BluetoothDevice device = result.getDevice(); // 进行相关处理,比如连接设备 } }); ``` 接下来,可以通过设备名称或MAC地址来连接设备,并与之进行数据通信。 ```java BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); BluetoothGatt gatt = device.connectGatt(this, false, new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { if (newState == BluetoothProfile.STATE_CONNECTED) { // 设备已连接 // 进行相关处理,比如发现服务 } else if (newState == BluetoothProfile.STATE_DISCONNECTED) { // 设备已断开连接 // 进行相关处理 } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { // 服务已发现 // 进行相关处理,比如获取特征值 } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { // 特征值已读取 // 进行相关处理 } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { // 特征值已写入 // 进行相关处理 } }); ``` 最后,可以使用BluetoothGatt对象向设备发送数据或者接收设备发送的数据。 ```java BluetoothGattCharacteristic characteristic = gatt.getService(serviceUuid).getCharacteristic(characteristicUuid); characteristic.setValue(data); gatt.writeCharacteristic(characteristic); ``` 这只是一个简单的示例,实际的BLE设备通信可能会涉及更多的操作和数据处理。使用上述代码作为起点,可以根据实际需求进行扩展和改进。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值