制作蓝牙小车

制作控制蓝牙小车app

想制作一个蓝牙小车,通过手机app程序操控小车运行,制作分三个部分(app制作,蓝牙小车硬件制作,小车程序制作),先完成第一个部分app制作,本次app是通过androidstudio软件来制作安卓应用程序

一、添加权限

在AndroidManifest.xml文件中添加权限

  <!-- 蓝牙操作权限 -->
    <uses-permission android:name="android.permission.BLUETOOTH"/>
    <!-- 蓝牙配对权限-->
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <!--仅在支持BLE(蓝牙4.0及以上)的设备上运行-->
    <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/>

    <!--如果Android6.0蓝牙搜索不到设备,需要补充以下两个权限-->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

    <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

二、设计界面

这里需要新建一个连接蓝牙的界面以及活动,这里新建的连接蓝牙活动取名Bluetooth_set

主界面
在这里插入图片描述
连接蓝牙界面
在这里插入图片描述
界面设计比较简单,无非就是布局和控件id设置

三、功能实现

MainActivity.java文件代码

package com.example.myapplication_ble_hc7;

import androidx.appcompat.app.AppCompatActivity;

import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

import static com.example.myapplication_ble_hc7.Bluetooth_set.bluetoothSocket;

public class MainActivity extends AppCompatActivity {
   

    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button_set =findViewById(R.id.button_set);
        button_set.setOnClickListener(new View.OnClickListener() {
   
            @Override
            public void onClick(View view) {
   
                Intent intent = new Intent(MainActivity.this, Bluetooth_set.class);
                startActivity(intent);//跳转到设置界面
            }
        });
        final Button button_go =findViewById(R.id.button_go);
        button_go.setBackgroundColor(Color.GREEN);
        final Button button_left =findViewById(R.id.button_left);
        button_left.setBackgroundColor(Color.GREEN);
        final Button button_right =findViewById(R.id.button_right);
        button_right.setBackgroundColor(Color.GREEN);
        final Button button_stop =findViewById(R.id.button_back);
        button_stop.setBackgroundColor(Color.GREEN);
        TextView textView =findViewById(R.id.textView2);
        if(bluetoothSocket==null){
   
            textView.setText("蓝牙未经连接");
            textView.setBackgroundColor(Color.RED);
        }else {
   
            textView.setText("蓝牙已经连接");
            textView.setBackgroundColor(Color.BLUE);
        }

            button_go.setOnTouchListener(new View.OnTouchListener() {
   
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
   
                switch (motionEvent.getAction()){
   
                    case MotionEvent.ACTION_DOWN:
                        send(1);
                        button_go.setBackgroundColor(Color.RED);
                        break;
                    case MotionEvent.ACTION_UP:
                        send(0);
                        button_go.setBackgroundColor(Color.GREEN);
                        break;


                }

                return true;
            }
        });
        button_left.setOnTouchListener(new View.OnTouchListener() {
   
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
   
                switch (motionEvent.getAction()){
   
                    case MotionEvent.ACTION_DOWN:
                        send(2);
                        button_left.setBackgroundColor(Color.RED);
                        break;
                    case MotionEvent.ACTION_UP:
                        send(0);
                        button_left.setBackgroundColor(Color.GREEN);
                        break;


                }

                return true;
            }
        });
        button_right.setOnTouchListener(new View.OnTouchListener() {
   
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
   
                switch (motionEvent.getAction()){
   
                    case MotionEvent.ACTION_DOWN:
                        send(3);
                        button_right.setBackgroundColor(Color.RED);
                        break;
                    case MotionEvent.ACTION_UP:
                        send(0);
                        button_right.setBackgroundColor(Color.GREEN);
                        break;


                }

                return true;
            }
        });
        button_stop.setOnTouchListener(new View.OnTouchListener() {
   
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
   
                switch (motionEvent.getAction()){
   
                    case MotionEvent.ACTION_DOWN:
                        send(4);
                        button_stop.setBackgroundColor(Color.RED);
                        break;
                    case MotionEvent.ACTION_UP:
                        send(0);
                        button_stop.setBackgroundColor(Color.GREEN);
                        break;


                }

                return true;
            }
        });



    }
    public void send(int intData){
   

        if(bluetoothSocket==null) {
   //先判断是否连接
            Toast.makeText(MainActivity.this,"设备未连接",Toast.LENGTH_SHORT).show();
        }else {
   
            try {
   
                bluetoothSocket.getOutputStream().write(intData);//建立数据库
            } catch (IOException e) {
    }
        }




    }
}

在Bluetooth_set.java文件中代码

package com.example.myapplication_ble_hc7;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Set;
import java.util.UUID;

public class Bluetooth_set extends AppCompatActivity {
   
    public static BluetoothSocket bluetoothSocket;
    UUID MY_UUID=UUID.fromString("00001101-0000-1000-8000-00805f9b34fb");//符合uuid格式就行
    ArrayList<String> ble_list =new ArrayList<>();//创建数组列表
    ArrayList<BluetoothDevice> ble=new ArrayList<>();//用来存放蓝牙设备
    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bluetooth_set);
        Button button_back = findViewById(R.id.button_back);
        ListView listView =findViewById(R.id.ble_list);
        button_back.setOnClickListener(new View.OnClickListener() {
   
            @Override
            public void onClick(View view) {
   
                Intent intent = new Intent(Bluetooth_set.this, MainActivity.class);
                startActivity(intent);//返回到主界面
            }
        });
        BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();//获取设备
        if (bluetoothAdapter == null) {
   //判断设备是否支持蓝牙
            Toast.makeText(Bluetooth_set.this, "注意:设备不支持蓝牙", Toast.LENGTH_SHORT).show();
        } else {
   
            Toast.makeText(Bluetooth_set.this, "设备支持蓝牙", Toast.LENGTH_SHORT).show();
        }
        if (!bluetoothAdapter.isEnabled()) {
    //判断设备是否打开蓝牙
             // bluetoothAdapter.enable();//打开蓝牙
            Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            startActivityForResult(enableBtIntent,1);   //通过意图打开蓝牙
        }
        Set<BluetoothDevice> device = bluetoothAdapter.getBondedDevices();//获取已经配对的设备,并存放到列表

        if(device.size()>0){
   
           for(BluetoothDevice mdevice:device){
   
               ble.add(mdevice);//添加蓝牙
               ble_list.add(mdevice.getName());//将获取的蓝牙名称添加到列表
           }
        }
        ArrayAdapter<String> view_list=new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,ble_list);//创建列表显示的适配器
        listView.setAdapter(view_list);//显示在列表里面
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
   
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
   
             //   BluetoothDevice bluetoothDevice =ble.get(i);//获取需要单击的蓝牙
                try {
   
                    bluetoothSocket=ble.get(i).createInsecureRfcommSocketToServiceRecord(MY_UUID);//获取需要单击的蓝牙,并且连接填入UUID
                    bluetoothSocket.connect();//蓝牙连接
                } catch (IOException e) {
   }
                Toast.makeText(Bluetooth_set.this, "蓝牙:"+ble.get(i).getName()+"已经连接", Toast.LENGTH_SHORT).show();

            }
        });



    }
}

四、效果呈现

把蓝牙先连接到电脑
在这里插入图片描述
安卓设备连接蓝牙并发送数据,下面是接收数据情况,我这边分别使用0,1,2,3,4表示停、前进、左转、右转、后退
在这里插入图片描述
第一阶段app程序暂时通过验证,接下来制作蓝牙小车

相关推荐

  1. C51--小车——串口/控制及点动

    2023-12-07 09:02:01       61 阅读
  2. 使用Android Studio制作一个软件

    2023-12-07 09:02:01       36 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-07 09:02:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-07 09:02:01       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-07 09:02:01       82 阅读
  4. Python语言-面向对象

    2023-12-07 09:02:01       91 阅读

热门阅读

  1. Android okhttp3.0配置https信任所有证书

    2023-12-07 09:02:01       58 阅读
  2. 「X」Embedding in NLP|初识自然语言处理(NLP)

    2023-12-07 09:02:01       53 阅读
  3. mongodb 日志详情

    2023-12-07 09:02:01       55 阅读
  4. 自然语言处理常用方法和评价指标

    2023-12-07 09:02:01       59 阅读
  5. 主流开源大语言模型的微调方法

    2023-12-07 09:02:01       53 阅读
  6. MongoDB归并连续号段-(待验证)

    2023-12-07 09:02:01       53 阅读
  7. 讲解机器学习中的 K-均值聚类算法及其优缺点。

    2023-12-07 09:02:01       58 阅读
  8. OpenFeign远程调用实例

    2023-12-07 09:02:01       58 阅读
  9. vue3+ts自定义插件

    2023-12-07 09:02:01       54 阅读
  10. Nginx实现(缓冲区)

    2023-12-07 09:02:01       57 阅读
  11. go - 计算CIDR的主机数量

    2023-12-07 09:02:01       60 阅读
  12. RefCell 数据类型

    2023-12-07 09:02:01       51 阅读
  13. Linux下打包Qt-使用linuxDeployQt+appimageTool

    2023-12-07 09:02:01       51 阅读