【TL8266】向蓝牙模块发送AT指令的APP

本文介绍如何在Android APP中实现向蓝牙模块发送AT指令。通过创建BleAtActivity,设置四个按钮分别对应连接、断开、发送指令和改名功能。在活动中,利用BluetoothLeService自定义服务进行设备操作。详细代码实现包括ServiceConnection的配置,用于根据蓝牙地址建立连接。
摘要由CSDN通过智能技术生成

前一篇文章写了如何显示BLE设备,在子项点击事件中只弹出了一个吐司提示点击的是哪个模块的地址,这一篇就将它改成发送指令

public class MainActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemClickListener{
   

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        BluetoothDevice device= (BluetoothDevice) mListViewAdapter.getItem(i);
        if (device==null)return;
        Intent intent=new Intent(this,BleAtActivity.class);
        intent.putExtra("name",device.getName());
        intent.putExtra("address",device.getAddress());
        startActivity(intent);
    }

}

跳转到BleAtActivity后,在这个页面放置四个按钮用于发送,连接,断开和改名
这里写图片描述

ble_at_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/send_button"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/connect_button"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/disconnect_button"/>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edit_name"/>

</LinearLayout>

延续之前的代码风格,用init函数写onCreate,由于只写发送,不接收,所以代码量很少,也没什么特别的,注意BluetoothLeService不是系统的类,是需要自定义的,用到了其中的一些连接的方法和发送的方法,在后面有全文(Ctrl键+F搜索BluetoothLeService.java和BleSppGattAttributes.java,直接复制即可),我一直没有机会重写它,有机会一定要自己写一个


public class BleAtActivity extends Activity implements View.OnClickListener{
   

    private int mLedStatus=0;

    private Button button;
    private Button connectButton;
    private Button disconnectButton;
    private Button editNameButton;

    private String mAddressString;
    private String mNameString;
    private BluetoothLeService mBluetoothLeService;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ble_at_layout);
        initView();
        initData();
        initEvent();

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(mServiceConnection);
        mBluetoothLeService = null;
    }

    private void initView(){
        button=findViewById(R.id.send_button);
        connectButton=findViewById(R.id.connect_button);
        disconnectButton=findViewById(R.id.disconnect_button);
        editNameButton=findViewById(R.id.edit_name);

    }

    private void initData(){

        mAddressString =getIntent().getStringExtra("address");
        mNameString=getIntent().getStringExtra("name");

        button.setText("开/关");
        connectButton.setText("连接");
        disconnectButton.setText("未连接");
        editNameButton.setText("改名");

    }

    private void initEvent(){

        button.setOnClickListener(this);
        connectButton.setOnClickListener(this);
        disconnectButton.setOnClickListener(this);
        editNameButton.setOnClickListener(this);

        Intent intent=new Intent(this,BluetoothLeService.class);
        bindService(intent,mServiceConnection,BIND_AUTO_CREATE);

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()){
            case R.id.send_button:
                if (mLedStatus==0){
                    sendAt(
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值