Android Studio制作手机App:通过手机蓝牙(Bluetooth)与STM32上的低功耗蓝牙(HC 您所在的位置:网站首页 蓝牙模块怎么连接单片机设备使用 Android Studio制作手机App:通过手机蓝牙(Bluetooth)与STM32上的低功耗蓝牙(HC

Android Studio制作手机App:通过手机蓝牙(Bluetooth)与STM32上的低功耗蓝牙(HC

2024-07-11 01:37| 来源: 网络整理| 查看: 265

背景:

本文的内容是针对单片机蓝牙模块(HC-42)开发的手机App。在这之前,我想先声明一点,手机与手机间的蓝牙连接方式”与“手机与HC间的蓝牙连接方式”是不一样的。原因就是手机搭配的是“经典蓝牙”模块,HC等蓝牙属于“低功耗蓝牙”模块。(二者的区别想了解的话建议你去看看其他朋友的文章),我在这里只想简单说一下这二者在功能代码实现上可以说是完全不一样的。这就解释了有一些朋友制作的软件明明可以与手机,平板等设备配对连接,却一直与HC蓝牙配对失败。

前言:

本文的内容只讲如何实现手机与HC蓝牙的配对,如果想了解一下手机与手机,手机与平板间的“经典蓝牙”通信方式,可以看我往期的博文,这篇博文讲的是如何制作一个基于蓝牙通信的聊天软件(类似于微信功能),也是一个挺有意思的项目(Android Studio制作蓝牙聊天通讯软件)

本文内容简介:

制作一个手机APP,无线连接HC蓝牙模块,将手机端数据发送给HC,从而控制STM32,文末会有资源分享。

简单通讯原理图:

如何制作这样一个App?

首先看一下本次的效果图:

 一、软件UI界面部分的设计实现

可以看 软件UI界面设计的实现 这篇博文,先实现界面设计后,再完成接下来的功能代码实现。

二、功能代码的实现。

1、在AndroidManifest.xml中添加依赖:

= Build.VERSION_CODES.M) { if (this.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, PERMISSION_REQUEST_COARSE_LOCATION); } } mbluetoothController.turnOnBlueTooth(); } //初始化列表,适配器的加载 public void Init_listView(){ mAdapter1 = new DeviceAdapter(MainActivity.this, R.layout.device_item, mbondDeviceList); ListView listView1 = (ListView)findViewById(R.id.listview1); listView1.setAdapter(mAdapter1); mAdapter1.notifyDataSetChanged(); mAdapter2 = new DeviceAdapter(MainActivity.this, R.layout.device_item, mfindDeviceList); ListView listView2 = (ListView)findViewById(R.id.listview2); listView2.setAdapter(mAdapter2); mAdapter2.notifyDataSetChanged(); listView2.setOnItemClickListener(toBondDevices);//点击设备,进行绑定 } //点击开始查找蓝牙设备 public View findDevice(View view){ //先执行其他代码,8s后执行run()里面的代码 mHandler.postDelayed(new Runnable() { @Override public void run() { change_Button_Text("搜索设备","ENABLE"); mbluetoothController.stopBlueTooth(myScanCallBack); } }, 8000); change_Button_Text("搜索中...","DISABLE"); mbluetoothController.scanBlueTooth(myScanCallBack);Log.e("提示","---------->findDevice"); return view; } //点击按键搜索后按键的变化 private void change_Button_Text(String text,String state){ if("ENABLE".equals(state)){ findBtn.setEnabled(true); findBtn.getBackground().setAlpha(255); //0~255 之间任意调整 findBtn.setTextColor(ContextCompat.getColor(this, R.color.black)); } else { findBtn.setEnabled(false); findBtn.getBackground().setAlpha(150); //0~255 之间任意调整 findBtn.setTextColor(ContextCompat.getColor(this, R.color.colorAccent)); } findBtn.setText(text); } //点击设备后执行的函数,跳转到第二个操作界面,并将选中的蓝牙设备信息传递过去 private AdapterView.OnItemClickListener toBondDevices =new AdapterView.OnItemClickListener(){ @Override public void onItemClick(AdapterView adapterView, View view, int i, long l){ mbluetoothController.stopBlueTooth(myScanCallBack); BluetoothDevice device = findDevices.get(i); Intent intent = new Intent(MainActivity.this,Main2Activity.class); intent.putExtra("device",device); startActivity(intent); } }; //设置toast的标准格式 private void showToast(String text){ if(mToast == null){ mToast = Toast.makeText(this, text,Toast.LENGTH_SHORT); mToast.show(); } else { mToast.setText(text); mToast.show(); } } }

4、MainActivity2 .java,完整代码及其解析如下:

package com.example.wyb.bluetoothchatui; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCallback; import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattService; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.List; import BluetoothPackage.BluetoothController; public class Main2Activity extends AppCompatActivity { private BluetoothDevice Device; private TextView textView; private EditText editText; private mGattCallback myGattCallBack; private BluetoothGatt mybluetoothGatt; private BluetoothController mbluetoothController; private String SERVICE_EIGENVALUE_SED = "0000ffe1-0000-1000-8000-00805f9b34fb"; private android.os.Handler mytimeHandler = new android.os.Handler(); private BluetoothGattCharacteristic mneedGattCharacteristic; private TextView deviceState; private TextView appname; @Override protected void onDestroy() { //返回第一个界面时取消蓝牙配对 mybluetoothGatt.disconnect(); super.onDestroy(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent intent = getIntent(); Device = intent.getParcelableExtra("device"); textView = (TextView) findViewById(R.id.textView1); editText = (EditText) findViewById(R.id.input_text); deviceState = (TextView) findViewById(R.id.device_state); appname = (TextView) findViewById(R.id.AppName); appname.setText(""); deviceState.setText("Device:"+Device.getName()+"(未连接)"); mbluetoothController = new BluetoothController(this); myGattCallBack = new mGattCallback(); mybluetoothGatt = mbluetoothController.connectBlueTooth(Device,false,myGattCallBack); } //蓝牙绑定功能的实现 private class mGattCallback extends BluetoothGattCallback { @Override public void onConnectionStateChange(BluetoothGatt gatt,int status,int newState){ Log.e("提示","蓝牙连接成功"); super.onConnectionStateChange(gatt,status,newState); mytimeHandler.postDelayed(new Runnable(){ @Override public void run(){ mybluetoothGatt.discoverServices(); deviceState.setText("Device:"+Device.getName()+"(已连接)"); } },1000); } @Override public void onServicesDiscovered(BluetoothGatt gatt,int status){ List services = mybluetoothGatt.getServices(); for(int i=0;i


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有