raspberry pi bluetooth 连接android手机
来源:互联网 发布:易语言蜘蛛池源码 编辑:程序博客网 时间:2024/06/05 17:46
板子:树莓派 zero wifi
系统:yocto编出的core-image-sato
安装python-pybluez:
dnf install python-pybluez
这样我们就可以:
import bluetooth
让树莓派可见并起名字为raspberry
hciconfig hci0 name "raspberry"hciconfig hci0 downhciconfig hci0 uphciconfig hci0 piscan
关键的一步:注册 Serial Port service
sudo sdptool add SP
树莓派作为server端的代码:
from bluetooth import *server_sock = BluetoothSocket(RFCOMM)server_sock.bind(("",PORT_ANY))server_sock.listen(1)port = server_sock.getsockname()[1]uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"advertise_service(server_sock, "TestServer", service_id=uuid,service_classes=[uuid,SERIAL_PORT_CLASS],profiles=[SERIAL_PORT_PROFILE],)client_sock, client_info=server_sock.accept()print client_infotry: while True: data=client_sock.recv(1024) print("received [%s]" % data)except IOError: passprint("disconnected")client_sock.close()server_sock.close()print("all done")
Android 端:
package com.example.wrsadmin.maker_raspberry;import android.Manifest;import android.bluetooth.BluetoothAdapter;import android.bluetooth.BluetoothDevice;import android.bluetooth.BluetoothSocket;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.content.pm.PackageManager;import android.os.Build;import android.support.v4.app.ActivityCompat;import android.support.v4.content.ContextCompat;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ListView;import android.widget.TextView;import java.io.IOException;import java.io.OutputStream;import java.util.ArrayList;import java.util.UUID;public class MainActivity extends AppCompatActivity { ArrayList mDevicesName = new ArrayList(); ArrayList<BluetoothDevice> mArrayListDevices; ArrayAdapter mArrayAdapter; Button b1; TextView tv_main; BluetoothAdapter mBluetoothAdapter; BluetoothDevice raspberry; BluetoothSocket raspberry_socket; ListView lv; UUID uuid=UUID.fromString("94f39d29-7d6d-437d-973b-fba39e49d4ee"); BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (BluetoothDevice.ACTION_FOUND.equals(action)) { BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if (device.getName()!=null) { if (!mDevicesName.contains(device.getName())) { mDevicesName.add(device.getName()); mArrayListDevices.add(device); if (device.getName().contains("raspberry")) { raspberry=device; } MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { lv.setAdapter(mArrayAdapter); } }); } } } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (ListView)findViewById(R.id.lv); b1 = (Button)findViewById(R.id.button); tv_main = (TextView)findViewById(R.id.textView); mArrayListDevices = new ArrayList<BluetoothDevice>(); mArrayAdapter = new ArrayAdapter(this, R.layout.listview,R.id.textView2,mDevicesName); // Enable Bluetooth mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (!mBluetoothAdapter.isEnabled()) { Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableBtIntent,1); } // Get Location Permissions if (Build.VERSION.SDK_INT >= 23) { if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); }; } // Register for broadcasts when a device is discovered. IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(mReceiver,filter); lv.setAdapter(mArrayAdapter); } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(mReceiver); } public void discoveryDevices(View view) { mDevicesName.clear(); mArrayListDevices.clear(); MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { lv.setAdapter(mArrayAdapter); } }); if (mBluetoothAdapter.isDiscovering()) { mBluetoothAdapter.cancelDiscovery(); mBluetoothAdapter.startDiscovery(); } else { mBluetoothAdapter.startDiscovery(); } } public void connectToRasp(View view) { if (raspberry!=null) { if (raspberry_socket==null) { try { raspberry_socket = raspberry.createInsecureRfcommSocketToServiceRecord(uuid); } catch (IOException e) { e.printStackTrace(); } } if (!raspberry_socket.isConnected()) { try { raspberry_socket.connect(); MainActivity.this.runOnUiThread(new Runnable() { @Override public void run() { tv_main.setText("Connected to raspberry!\n"); } }); } catch (IOException e) { e.printStackTrace(); } } } } public void Turnleft(View view) { if(raspberry_socket!=null) { if (raspberry_socket.isConnected()) { String s = "left"; byte[] buffer = s.getBytes(); try { OutputStream os=raspberry_socket.getOutputStream(); os.write(buffer); os.flush(); } catch (Exception e) { e.printStackTrace(); } } } } public void Turnright(View view) { if(raspberry_socket!=null) { if (raspberry_socket.isConnected()) { String s = "right"; byte[] buffer = s.getBytes(); try { OutputStream os=raspberry_socket.getOutputStream(); os.write(buffer); os.flush(); } catch (Exception e) { e.printStackTrace(); } } } } public void duojiStop(View view) { if(raspberry_socket!=null) { if (raspberry_socket.isConnected()) { String s = "duojistop"; byte[] buffer = s.getBytes(); try { OutputStream os=raspberry_socket.getOutputStream(); os.write(buffer); os.flush(); } catch (Exception e) { e.printStackTrace(); } } } } public void Forward(View view) { if(raspberry_socket!=null) { if (raspberry_socket.isConnected()) { String s = "forward"; byte[] buffer = s.getBytes(); try { OutputStream os=raspberry_socket.getOutputStream(); os.write(buffer); os.flush(); } catch (Exception e) { e.printStackTrace(); } } } } public void Back(View view) { if(raspberry_socket!=null) { if (raspberry_socket.isConnected()) { String s = "back"; byte[] buffer = s.getBytes(); try { OutputStream os=raspberry_socket.getOutputStream(); os.write(buffer); os.flush(); } catch (Exception e) { e.printStackTrace(); } } } } public void dianjiStop(View view) { if(raspberry_socket!=null) { if (raspberry_socket.isConnected()) { String s = "dianjistop"; byte[] buffer = s.getBytes(); try { OutputStream os=raspberry_socket.getOutputStream(); os.write(buffer); os.flush(); } catch (Exception e) { e.printStackTrace(); } } } }}
关于问题:Failed to connect to SDP server on FF:FF:FF:00:00:00: No such file or directory的解法
https://bbs.archlinux.org/viewtopic.php?id=201672
阅读全文
0 0
- raspberry pi bluetooth 连接android手机
- Raspberry Pi: SSH连接Raspberry Pi
- 树莓派安装BlueZ协议栈(Raspberry pi Bluetooth LE)
- raspberry pi zero w: yocto image 启动bluetooth
- [raspberry pi] 如何设置WIFI连接
- raspberry pi 的 有线网络连接
- raspberry pi的无线网卡连接
- HC-SR04 连接 Raspberry Pi B+
- Raspberry Pi 树莓派连接温度传感器DS18B20
- Raspberry Pi移植Android记录(一)
- Raspberry Pi 3刷入Android Things
- 树莓派(raspberry pi)学习15: 使用WIFI网卡连接无线网络
- 树莓派(Raspberry Pi)USB无线网卡自动连接
- 远程连接Raspberry Pi(数莓派)图形用户界面(X Window)
- 在raspberry pi(树莓派)上连接VPN
- raspberry pi zero通过usb进行ssh连接
- raspberry Pi zero v1.3 设置无线wifi连接
- raspberry pi新系统SSH连接被拒绝的解决方法
- Ant编译多个依赖工程jar、war包
- 欢迎使用CSDN-markdown编辑器
- 软件调试笔记47
- Mybatis-HelloWorld
- Java之二维码工具类
- raspberry pi bluetooth 连接android手机
- 锐捷RG-BDS,态势感知技术助力网络安全前行
- google 相册数据迁移至另一google账号方法
- [ 杂题 ] BZOJ5085
- 动态内存与智能指针
- React Native
- leveldb源码分析
- Excel
- MySQL可以存放的数据类型