先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7
深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
如果你需要这些资料,可以添加V获取:vip204888 (备注Android)
正文
step2、获取蓝牙适配器
BluetoothManager mBluetoothManager =(BluetoothManager)context.getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter mBluetoothAdapter = mBluetoothManager.getAdapter();
- 如果mBluetoothAdapter为空,是因为手机蓝牙不支持与ble设备通讯,换句话说就是安卓手机系统在4.3以下了。
step3、判断手机蓝牙是否被打开
mBluetoothAdapter.isEnabled()
-
如果返回true,这个时候就可以扫描了
-
如果返回false,这时候需要打开手机蓝牙。 可以调用系统方法让用户打开蓝牙。
Intent enable = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(enable);
2、搜索蓝牙
step1、开始扫描
//10s后停止搜索
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
mBluetoothAdapter.stopLeScan(mLeScanCallback);
}
}, 1000 * 10);
UUID[] serviceUuids = {UUID.fromString(service_uuid)};
mBluetoothAdapter.startLeScan(serviceUuids, mLeScanCallback);
-
startLeScan中,第一个参数是只扫描UUID是同一类的ble设备,第二个参数是扫描到设备后的回调。
-
因为蓝牙扫描比较耗电,建议设置扫描时间,一定时间后停止扫描。
-
如果不需要过滤扫描到的蓝牙设备,可用
mBluetoothAdapter.startLeScan(mLeScanCallback);
进行扫描。
step2、扫描的回调
//蓝牙扫描回调接口
private BluetoothAdapter.LeScanCallback mLeScanCallback = new BluetoothAdapter.LeScanCallback(){
@Override
public void onLeScan(final BluetoothDevice device, int rssi, byte[] scanRecord) {
if (device.getName() == null) {
return;
}
Log.e(“—>搜索到的蓝牙名字:”, device.getName());
//可以将扫描的设备弄成列表,点击设备连接,也可以根据每个设备不同标识,自动连接。
}
};
3、连接蓝牙
step1、获取设备的mac地址,然后连接。
//获取所需地址
String mDeviceAddress = device.getAddress();
BluetoothGatt mBluetoothGatt = device.connectGatt(context, false, mGattCallback);
step2、onConnectionStateChange()被调用
- 连接状态改变时,mGattCallback中onConnectionStateChange()方法会被调用,当连接成功时,需要调用
mBluetoothGatt.discoverServices();
去获取服务。
step3、onServicesDiscovered()被调用
-
调用
mBluetoothGatt.discoverServices();
方法后,onServicesDiscovered()
这个方法会被调用,说明发现当前设备了。然后我们就可以在里面去获取BluetoothGattService和BluetoothGattCharacteristic。 -
下面就是mGattCallback回调方法。
// BLE回调操作
private BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onConnectionStateChange(BluetoothGatt gatt, int status,int newState){
super.onConnectionStateChange(gatt, status, newState);
if (newState == BluetoothProfile.STATE_CONNECTED) {
// 连接成功
mBluetoothGatt.discoverServices();
} else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
// 连接断开
Log.d(“TAG”,“onConnectionStateChange fail–>” + status);
}
}
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
super.onServicesDiscovered(gatt, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
//发现设备,遍历服务,初始化特征
initBLE(gatt);
} else {
Log.d(“TAG”,“onServicesDiscovered fail–>” + status);
}
}
@Override
public void onCharacteristicRead(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status){
super.onCharacteristicRead(gatt, characteristic, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
// 收到的数据
byte[] receiveByte = characteristic.getValue();
}else{
Log.d(“TAG”,“onCharacteristicRead fail–>” + status);
}
}
@Override
public void onCharacteristicChanged(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic){
super.onCharacteristicChanged(gatt, characteristic);
//当特征中value值发生改变
}
/**
-
收到BLE终端写入数据回调
-
@param gatt
-
@param characteristic
-
@param status
*/
@Override
public void onCharacteristicWrite(BluetoothGatt gatt,BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
// 发送成功
} else {
// 发送失败
}
}
@Override
public void onDescriptorWrite(BluetoothGatt gatt,
BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorWrite(gatt, descriptor, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
}
}
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status) {
super.onReadRemoteRssi(gatt, rssi, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
}
}
@Override
public void onDescriptorRead(BluetoothGatt gatt,BluetoothGattDescriptor descriptor, int status) {
super.onDescriptorRead(gatt, descriptor, status);
if (status == BluetoothGatt.GATT_SUCCESS) {
}
}
};
4、获取特征
step1、ble设备相关的UUID
//写通道uuid
private static final UUID writeCharactUuid = UUID.fromString(“0000fff6-0000-1000-8000-00805f9b34fb”);
//通知通道 uuid
private static final UUID notifyCharactUuid =UUID.fromString( “0000fff7-0000-1000-8000-00805f9b34fb”);
- 不同的ble设备的UUID不相同,请根据自己的设备初始化UUID。
step2、获取bluetoothGattCharacteristic(因为有的设备可能存在双服务的情况,所以这里遍历所有服务)
//初始化特征
public void initBLE(BluetoothGatt gatt) {
if (gatt == null) {
return;
}
//遍历所有服务
for (BluetoothGattService BluetoothGattService : gatt.getServices()) {
Log.e(TAG, “—>BluetoothGattService” + BluetoothGattService.getUuid().toString());
//遍历所有特征
for (BluetoothGattCharacteristic bluetoothGattCharacteristic : BluetoothGattService.getCharacteristics()) {
Log.e(“---->gattCharacteristic”, bluetoothGattCharacteristic.getUuid().toString());
String str = bluetoothGattCharacteristic.getUuid().toString();
if (str.equals(writeCharactUuid)) {
//根据写UUID找到写特征
mBluetoothGattCharacteristic = bluetoothGattCharacteristic;
} else if (str.equals(notifyCharactUuid)) {
//根据通知UUID找到通知特征
mBluetoothGattCharacteristicNotify = bluetoothGattCharacteristic;
}
最后
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。希望能够帮助到大家提升技术
高级UI,自定义View
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注Android)
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
链图片转存中…(img-fH2c3gcJ-1713709288317)]
高级UI,自定义View
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
[外链图片转存中…(img-SeLdxfYX-1713709288317)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
需要这份系统化的资料的朋友,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-yNHdym1k-1713709288318)]
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!