制作控制蓝牙小车app
想制作一个蓝牙小车,通过手机app程序操控小车运行,制作分三个部分(app制作,蓝牙小车硬件制作,小车程序制作),先完成第一个部分app制作,本次app是通过androidstudio软件来制作安卓应用程序
一、添加权限
在AndroidManifest.xml文件中添加权限
java
<!-- 蓝牙操作权限 -->
<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文件代码
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文件中代码
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程序暂时通过验证,接下来制作蓝牙小车