使用蓝牙从Raspberry Pi向Android发送字符串

时间:2018-11-19 11:25:18

标签: android bluetooth raspberry-pi android-bluetooth pybluez

我正在尝试使用蓝牙从覆盆子pi向Android手机发送字符串。我可以将两个设备连接起来,但是在电话上接收数据时遇到了一些麻烦。这是我使用pybluez在树莓派上的python代码:

groovy.mock.interceptor

这是我的android代码中要接收数据的部分:

import groovy.mock.interceptor.MockFor
import org.junit.Test

class MyClassTest extends GroovyTestCase {
    @Test
    void test_correctness_of_passed_arguments() {
        def mock = new MockFor(MyClass)
        mock.ignore('main')
        mock.demand.myFunction{a, b, c -> '0'} // Is this where I should enforce the input params?
        mock.use {
            def foo = new MyClass()
            foo.main()  // <--- this is in there that it gets executed
        }
        mock.expect.verify()
        mock.demand.recorded[0] // <--- can I get what has been passed afterwards?
    }
}

这是程序在pi上运行时得到的:

from bluetooth import *
import time

server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

uuid = "00001101-0000-1000-8000-00805F9B34FB"

advertise_service( server_sock, "SampleServer",
               service_id = uuid,
               service_classes = [ uuid, SERIAL_PORT_CLASS ],
               profiles = [ SERIAL_PORT_PROFILE ],#, 
#              protocols = [ OBEX_UUID ] 
              )

print ("Waiting for connection on RFCOMM channel %d" % port)

client_sock, client_info = server_sock.accept()
print ("Accepted connection from ", client_info)



while True:
    try:
        print("Preparing to send data\n")
        server_sock.send("This is a test of the android raspberry pi Bluetooth Connection\n")
        print("Data sent\n")
        time.sleep(0.5)


    except IOError:
        pass

print ("disconnected")

client_sock.close()
server_sock.close()
print ("all done")

在Android设备上重新创建从树莓派发送的字符串的最佳方法是什么?

0 个答案:

没有答案