androidstudio没有蓝牙模块
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了androidstudio没有蓝牙模块相关的知识,希望对你有一定的参考价值。
参考技术A androidstudio连接步骤如下:1、设置权限。
2、客户端开启蓝牙。
3、注册搜索蓝牙receiver。
4、客户端请求连接。
5、服务器端开启蓝牙,服务器端等待连接即可。
如何简单地将字符串从 android studio (kotlin) 传递到 arduino 串行蓝牙模块 (HC-05)?
【中文标题】如何简单地将字符串从 android studio (kotlin) 传递到 arduino 串行蓝牙模块 (HC-05)?【英文标题】:How to simply pass a string from android studio (kotlin) to arduino serial bluetooth module (HC-05)? 【发布时间】:2019-07-03 02:43:27 【问题描述】: 我正在使用 Android Studio (Kotlin) 为学校项目制作一个 Android 应用。 我需要通过 HC-05 蓝牙模块向 Arduino Genuino Uno 模块发送字符串。 当应用程序启动时,Arduino 已经连接(配对)到我的 Android 设备。 谁能帮我找到一个正确且简单的方法来只发送这些数据? 非常感谢。【问题讨论】:
你可以使用类似 ***.com/questions/22899475/… 的东西,它是用 Java 编写的,但可以提供帮助。 谢谢,我试试这个。 【参考方案1】:我终于得到了答案,我做到了:
private fun CheckBt()
Toast.makeText(applicationContext, "It has started", Toast.LENGTH_SHORT).show()
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
if (!mBluetoothAdapter.enable())
Toast.makeText(applicationContext, "Bluetooth Disabled !", Toast.LENGTH_SHORT).show()
/* It tests if the bluetooth is enabled or not, if not the app will show a message. */
finish()
if (mBluetoothAdapter == null)
Toast.makeText(applicationContext, "Bluetooth null !", Toast.LENGTH_SHORT).show()
fun Connect()
val device = mBluetoothAdapter.getRemoteDevice("98:D3:32:71:17:DE")
Log.d("", "Connecting to ... $device")
Toast.makeText(applicationContext, "Connecting to ... $device.name mac: $device.uuids[0] address: $device.address", Toast.LENGTH_LONG).show()
mBluetoothAdapter.cancelDiscovery()
try
btSocket = device.createRfcommSocketToServiceRecord(myUUID)
/* Here is the part the connection is made, by asking the device to create a RfcommSocket (Unsecure socket I guess), It map a port for us or something like that */
btSocket.connect()
Log.d("", "Connection made.")
Toast.makeText(applicationContext, "Connection made.", Toast.LENGTH_SHORT).show()
catch (e: IOException)
try
btSocket.close()
catch (e2: IOException)
Log.d("", "Unable to end the connection")
Toast.makeText(applicationContext, "Unable to end the connection", Toast.LENGTH_SHORT).show()
Log.d("", "Socket creation failed")
Toast.makeText(applicationContext, "Socket creation failed", Toast.LENGTH_SHORT).show()
//beginListenForData()
/* this is a method used to read what the Arduino says for example when you write Serial.print("Hello world.") in your Arduino code */
private fun writeData(data: String)
var outStream = btSocket.outputStream
try
outStream = btSocket.outputStream
catch (e: IOException)
//Log.d(FragmentActivity.TAG, "Bug BEFORE Sending stuff", e)
val msgBuffer = data.toByteArray()
try
outStream.write(msgBuffer)
catch (e: IOException)
//Log.d(FragmentActivity.TAG, "Bug while sending stuff", e)
【讨论】:
以上是关于androidstudio没有蓝牙模块的主要内容,如果未能解决你的问题,请参考以下文章
HC-06蓝牙模块在发送AT指令设置时怎么没有任何反应??不是应该返回OK吗?