通过蓝牙将字符串从作为客户端的 PC 发送到作为服务器的移动设备

2024-05-26

我需要通过蓝牙将字符串从 PC 传输到 Android 移动设备的帮助。 Android 移动设备应充当服务器并在设备屏幕上显示字符串消息。作为客户端的 PC 应该将字符串发送到移动设备。

我希望服务器对提取的字符串(通过蓝牙传输)做出反应。这意味着服务器一方面必须始终侦听新字符串的到达,但另一方面仍然必须能够对这些消息做出反应(例如从一个菜单导航到另一个菜单)。

我尝试使用 BlueCove (2.1.1) 作为 BluetoothStack(为此,我将 BlueCove 中的 jar 作为库添加到两个项目中)并结合我发现的服务器客户端通信示例here http://www.jsr82.com/jsr-82-sample-spp-server-and-client/.

Updates:

来自服务器的更新代码感谢user_CC用一个RFComm服务器的连接:

public class RFCommServer extends Thread{

//based on java.util.UUID
private static UUID MY_UUID = UUID.fromString("446118f0-8b1e-11e2-9e96-0800200c9a66");

// The local server socket
private BluetoothServerSocket mmServerSocket;

// based on android.bluetooth.BluetoothAdapter
private BluetoothAdapter mAdapter;
private BluetoothDevice remoteDevice;

private Activity activity;

public RFCommServer(Activity activity) {
    this.activity = activity;
}

public void run() {
    BluetoothSocket socket = null;
    mAdapter = BluetoothAdapter.getDefaultAdapter();        

    // Listen to the server socket if we're not connected
    while (true) {

        try {
            // Create a new listening server socket
            Log.d(this.getName(), ".....Initializing RFCOMM SERVER....");

            // MY_UUID is the UUID you want to use for communication
            mmServerSocket = mAdapter.listenUsingRfcommWithServiceRecord("MyService", MY_UUID);
            //mmServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID); // you can also try using In Secure connection...

            // This is a blocking call and will only return on a
            // successful connection or an exception
            socket = mmServerSocket.accept();

        } catch (Exception e) {

        }

        try {
            Log.d(this.getName(), "Closing Server Socket.....");
            mmServerSocket.close();

            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams

            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();

            DataInputStream mmInStream = new DataInputStream(tmpIn);
            DataOutputStream mmOutStream = new DataOutputStream(tmpOut);

            // here you can use the Input Stream to take the string from the client whoever is connecting
            //similarly use the output stream to send the data to the client

            RelativeLayout layout = (RelativeLayout) activity.findViewById(R.id.relativeLayout_Layout);
            TextView text = (TextView) layout.findViewById(R.id.textView_Text);

            text.setText(mmInStream.toString());
        } catch (Exception e) {
            //catch your exception here
        }
    }
}

SPP 客户端的代码来自here http://www.jsr82.com/jsr-82-sample-spp-server-and-client/:

/**
* A simple SPP client that connects with an SPP server
*/
public class SampleSPPClient implements DiscoveryListener{

//object used for waiting
private static Object lock=new Object();

//vector containing the devices discovered
private static Vector vecDevices=new Vector();

private static String connectionURL=null;

public static void main(String[] args) throws IOException {

    SampleSPPClient client=new SampleSPPClient();

    //display local device address and name
    LocalDevice localDevice = LocalDevice.getLocalDevice();
    System.out.println("Address: "+localDevice.getBluetoothAddress());
    System.out.println("Name: "+localDevice.getFriendlyName());

    //find devices
    DiscoveryAgent agent = localDevice.getDiscoveryAgent();

    System.out.println("Starting device inquiry...");
    agent.startInquiry(DiscoveryAgent.GIAC, client);

    try {
        synchronized(lock){
            lock.wait();
        }
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }


    System.out.println("Device Inquiry Completed. ");

    //print all devices in vecDevices
    int deviceCount=vecDevices.size();

    if(deviceCount <= 0){
        System.out.println("No Devices Found .");
        System.exit(0);
    }
    else{
        //print bluetooth device addresses and names in the format [ No. address (name) ]
        System.out.println("Bluetooth Devices: ");
        for (int i = 0; i <deviceCount; i++) {
            RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(i);
            System.out.println((i+1)+". "+remoteDevice.getBluetoothAddress()+" ("+remoteDevice.getFriendlyName(true)+")");
        }
    }

    System.out.print("Choose Device index: ");
    BufferedReader bReader=new BufferedReader(new InputStreamReader(System.in));

    String chosenIndex=bReader.readLine();
    int index=Integer.parseInt(chosenIndex.trim());

    //check for spp service
    RemoteDevice remoteDevice=(RemoteDevice)vecDevices.elementAt(index-1);
    UUID[] uuidSet = new UUID[1];
    uuidSet[0]=new UUID("446118f08b1e11e29e960800200c9a66", false);

    System.out.println("\nSearching for service...");
    agent.searchServices(null,uuidSet,remoteDevice,client);

    try {
        synchronized(lock){
            lock.wait();
        }
    }
    catch (InterruptedException e) {
        e.printStackTrace();
    }

    if(connectionURL==null){
        System.out.println("Device does not support Simple SPP Service.");
        System.exit(0);
    }

    //connect to the server and send a line of text
    StreamConnection streamConnection=(StreamConnection)Connector.open(connectionURL);

    //send string
    OutputStream outStream=streamConnection.openOutputStream();
    PrintWriter pWriter=new PrintWriter(new OutputStreamWriter(outStream));
    pWriter.write("Test String from SPP Client\r\n");
    pWriter.flush();


    //read response
    InputStream inStream=streamConnection.openInputStream();
    BufferedReader bReader2=new BufferedReader(new InputStreamReader(inStream));
    String lineRead=bReader2.readLine();
    System.out.println(lineRead);


}//main

//methods of DiscoveryListener
public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod) {
    //add the device to the vector
    if(!vecDevices.contains(btDevice)){
        vecDevices.addElement(btDevice);
    }
}

//implement this method since services are not being discovered
public void servicesDiscovered(int transID, ServiceRecord[] servRecord) {
    if(servRecord!=null && servRecord.length>0){
        connectionURL=servRecord[0].getConnectionURL(0,false);
    }
    synchronized(lock){
        lock.notify();
    }
}

//implement this method since services are not being discovered
public void serviceSearchCompleted(int transID, int respCode) {
    synchronized(lock){
        lock.notify();
    }
}


public void inquiryCompleted(int discType) {
    synchronized(lock){
        lock.notify();
    }

}//end method

}

为了进行测试,我使用具有最新 Android API 的 Galaxy Nexus (GT-I9250)。

谢谢user_CC,客户端和服务器现在运行没有异常。但遗憾的是客户端无法连接到服务器(请参见下面的屏幕截图)。这是因为connectionURL从未设置(因此它跳到这里if(connectionURL==null)默认情况下。

如何更改客户端代码,以便我实际上可以将其与服务器连接?我需要一个合适的connectionURL在下面一行中:

StreamConnection streamConnection=(StreamConnection)Connector.open(connectionURL)

到目前为止我只发现我需要以某种方式获得ServiceRecord,遗憾的是,示例代码中也没有描述这一点here http://www.jsr82.com/jsr-82-sample-spp-server-and-client/.


您将需要使用 RFComm APIS 来进行通信工作,我已设法定义一个类,该类是一个线程,并将充当服务器并侦听客户端连接。我也发表了一些评论供您理解。

    private class AcceptThread extends Thread {
    // The local server socket
    private BluetoothServerSocket mmServerSocket;

    public AcceptThread() {
    }

    public void run() {         
        BluetoothSocket socket = null;

                    BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();

        // Listen to the server socket if we're not connected
        while (true) {

            try {
                // Create a new listening server socket
                Log.d(TAG, ".....Initializing RFCOMM SERVER....");

                // MY_UUID is the UUID you want to use for communication
                mmServerSocket = mAdapter.listenUsingRfcommWithServiceRecord(NAME, MY_UUID);                    
                //mmServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME, MY_UUID);  you can also try using In Secure connection...

                // This is a blocking call and will only return on a
                // successful connection or an exception                    
                socket = mmServerSocket.accept();                   

            } catch (Exception e) {

            }

            try {
                Log.d(TAG, "Closing Server Socket.....";                    
                mmServerSocket.close();



                InputStream tmpIn = null;
                OutputStream tmpOut = null;

                // Get the BluetoothSocket input and output streams

                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();


                mmInStream = new DataInputStream(tmpIn);
                mmOutStream = new DataOutputStream(tmpOut); 

                // here you can use the Input Stream to take the string from the client whoever is connecting
                //similarly use the output stream to send the data to the client
            } catch (Exception e) {
                //catch your exception here
            }

        }
    }

}

我希望这有帮助

对于你的另一个问题:

在客户端 (PC) UUID 类上声明 javax.bluetooth.UUID 应来自 javax.bluetooth.UUID

   uuidSet2[0] = new UUID("446118f08b1e11e29e960800200c9a66", false);

在服务器端声明 java.util.UUID (Android)

    UUID MY_UUID = UUID.fromString("446118f0-8b1e-11e2-9e96-0800200c9a66");
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过蓝牙将字符串从作为客户端的 PC 发送到作为服务器的移动设备 的相关文章

  • 路径中 File.separator 和斜杠之间的区别

    使用有什么区别File separator和一个正常的 在 Java 路径字符串中 与双反斜杠相反 平台独立性似乎不是原因 因为两个版本都可以在 Windows 和 Unix 下运行 public class SlashTest Test
  • Spring @RequestMapping 带有可选参数

    我的控制器在请求映射中存在可选参数的问题 请查看下面的控制器 GetMapping produces MediaType APPLICATION JSON VALUE public ResponseEntity
  • 无法解析插件 Java Spring

    我正在使用 IntelliJ IDEA 并且我尝试通过 maven 安装依赖项 但它给了我这些错误 Cannot resolve plugin org apache maven plugins maven clean plugin 3 0
  • 斯坦福 NLP - 处理文件列表时 OpenIE 内存不足

    我正在尝试使用斯坦福 CoreNLP 中的 OpenIE 工具从多个文件中提取信息 当多个文件 而不是一个 传递到输入时 它会给出内存不足错误 All files have been queued awaiting termination
  • 如何为俚语和表情符号构建正则表达式 (regex)

    我需要构建一个正则表达式来匹配俚语 即 lol lmao imo 等 和表情符号 即 P 等 我按照以下示例进行操作http www coderanch com t 497238 java java Regular Expression D
  • 如何使用InputConnectionWrapper?

    我有一个EditText 现在我想获取用户对此所做的所有更改EditText并在手动将它们插入之前使用它们EditText 我不希望用户直接更改中的文本EditText 这只能由我的代码完成 例如通过使用replace or setText
  • 尝试在 ubuntu 中编译 android 内核时出错

    我正在尝试从源代码编译 Android 内核 并且我已经下载了所有正确的软件包来执行此操作 但由于某种原因我收到此错误 arm linux androideabi gcc error unrecognized command line op
  • 总是使用 Final?

    我读过 将某些东西做成最终的 然后在循环中使用它会带来更好的性能 但这对一切都有好处吗 我有很多地方没有循环 但我将 Final 添加到局部变量中 它会使速度变慢还是仍然很好 还有一些地方我有一个全局变量final 例如android Pa
  • Java Integer CompareTo() - 为什么使用比较与减法?

    我发现java lang Integer实施compareTo方法如下 public int compareTo Integer anotherInteger int thisVal this value int anotherVal an
  • Java列表的线程安全

    我有一个列表 它将在线程安全上下文或非线程安全上下文中使用 究竟会是哪一个 无法提前确定 在这种特殊情况下 每当列表进入非线程安全上下文时 我都会使用它来包装它 Collections synchronizedList 但如果不进入非线程安
  • 编译器抱怨“缺少返回语句”,即使不可能达到缺少返回语句的条件

    在下面的方法中 编译器抱怨缺少退货声明即使该方法只有一条路径 并且它包含一个return陈述 抑制错误需要另一个return陈述 public int foo if true return 5 鉴于Java编译器可以识别无限循环 https
  • 在 Maven 依赖项中指定 jar 和 test-jar 类型

    我有一个名为 commons 的项目 其中包含运行时和测试的常见内容 在主项目中 我添加了公共资源的依赖项
  • 实现滚动选择 ListView 中的项目

    我想使用 ListView 您可以在其中滚动列表来选择一个项目 它应该像一个 Seekbar 但拇指应该是固定的 并且您必须使用该栏来调整它 我面临的一个问题是 我不知道这种小部件是如何调用的 这使得我很难搜索 所以我制作了下面这张图片 以
  • 捕获的图像分辨率太大

    我在做什么 我允许用户捕获图像 将其存储到 SD 卡中并上传到服务器 但捕获图像的分辨率为宽度 4608 像素和高度 2592 像素 现在我想要什么 如何在不影响质量的情况下获得小分辨率图像 例如我可以获取或设置捕获的图像分辨率为原始图像分
  • 使用 JMF 创建 RTP 流时出现问题

    我正处于一个项目的早期阶段 需要使用 RTP 广播DataStream创建自MediaLocation 我正在遵循一些示例代码 该代码目前在rptManager initalize localAddress 出现错误 无法打开本地数据端口
  • JGit 检查分支是否已签出

    我正在使用 JGit 开发一个项目 我设法删除了一个分支 但我还想检查该分支是否已签出 我发现了一个变量CheckoutCommand但它是私有的 private boolean isCheckoutIndex return startCo
  • 按日期对 RecyclerView 进行排序

    我正在尝试按日期对 RecyclerView 进行排序 但我尝试了太多的事情 我不知道现在该尝试什么 问题就出在这条线上适配器 notifyDataSetChanged 因为如果我不放 不会显示错误 但也不会更新 recyclerview
  • 节拍匹配算法

    我最近开始尝试创建一个移动应用程序 iOS Android 它将自动击败比赛 http en wikipedia org wiki Beatmatching http en wikipedia org wiki Beatmatching 两
  • Spring Boot @ConfigurationProperties 不从环境中检索属性

    我正在使用 Spring Boot 1 2 1 并尝试创建一个 ConfigurationProperties带有验证的bean 如下所示 package com sampleapp import java net URL import j
  • 强制 Listview 不重复使用视图(复选框)

    我做了一个定制Listview 没有覆盖getView 方法 Listview 中的每个项目都具有以下布局 联系布局 xml

随机推荐

  • 有没有办法创建与元素不同的盒子阴影形状?

    当我将鼠标悬停在复选框输入上时 我尝试在复选框输入上创建圆形框阴影 但它采用元素的形状 如何将框阴影的形状更改为圆形
  • 将变量作为 IIFE 中的属性返回

    我试图在 IIFE 初始化后返回一个变量集作为属性 问题是 如果我直接绑定变量 我会得到一个空对象 如果我通过函数绑定它 我就会得到我想要的结果 var Application function var localInformation f
  • eBay API 调用不适用于 UPC/EAN

    eBay 的 API findItemsByProduct 操作适用于 UPC 和 EAN 但不幸的是它不起作用 例如 下面的 HTTP GET 请求会抛出 无效的产品 ID 值 错误41 Note 请将 SECURITY APPNAME
  • 发送带有附件的 PHP HTML 邮件

    我遇到了一个问题 直到今天 我使用 PHP 发送 HTML 电子邮件 其中包含的标头 Content type text html 现在 我添加了添加附件的功能 为此 我必须将此行更改为 Content Type multipart mix
  • 应该有 还是 应该有

    我正在读马克 皮尔格姆的 深入研究 HTML5 http www diveintohtml5 net semantics html 并且在语义部分 http www diveintohtml5 net semantics html new
  • WebView 的自定义用户代理

    我可以为一个设置自定义用户代理吗WebView 我需要展示网站的移动风格 这很容易做到 string ua Mozilla 5 0 iPhone CPU iPhone OS 6 0 like Mac OS X AppleWebKit 536
  • Python daysBetweenDate

    我想我可能有一个无限循环 因为每当我运行代码时 我都会收到一条错误消息 它说 程序因使用 13 CPU 秒而关闭 整个代码 应该以日期作为输入并输出第二天 此代码假设所有月份都是 30 天 除了daysBetweenDates功能正常 其他
  • Spark Workers 上缺少 SLF4J 记录器

    我正在尝试通过以下方式运行工作spark submit 此作业导致的错误是 Exception in thread main java lang NoClassDefFoundError org slf4j Logger at java l
  • 使用连接字段的 SQL JOIN

    我有两个表 Table1 包含一列 该列构成 Table2 中列的部分值 例如 表1 XName 123456 表2 ZName ABC 123456 我需要创建一个与这些匹配的 JOIN 但是使用 MS SQL 2008 我在完成这项工作
  • log4j2 SMTP Appender:如何包含另一个级别的先前消息?

    我正在使用 log4j2 beta9 并且有以下配置 其中一部分
  • 调用另一个 PHP 脚本并在其他脚本完成之前将控制权返回给用户

    我实际上正在尝试将其应用于发送电子邮件的脚本 发送电子邮件部分需要几秒钟 这太长了 我想要的是第一个脚本执行其操作并触发另一个脚本 发送电子邮件 但我希望第一个脚本将控制权返回给用户 而无需等待第二个脚本发送电子邮件 我考虑过的选项 计划任
  • Azure Active Directory 不会使用 ASP.NET Core 2.1 MVC 注销

    我有一个 ASP NET Core 2 1 MVC 应用程序 我正在尝试使用 Azure AD 进行身份验证 该应用程序重定向到 Microsoft 登录页面 但当我注销然后返回到应用程序的主页时 它会自动重新登录 我试过打电话https
  • LineSpace 如何影响单行文本中的 StaticLayout 高度

    考虑这个简单的例子 我有这样的单行文本 你好 我想使用 StaticLayout 来测量这个文本 所以我写了这样的东西 StaticLayout layout new StaticLayout Hello myTextView getPai
  • 在 JavaScript 中使用随机数创建长度为 n 的数组

    跟进这个答案 https stackoverflow com a 34693778 1525840为了创建指定长度的数组 我执行了以下命令以获得相应的结果 但填充了随机数 而不是零 var randoms Array 4 fill Math
  • 是否可以创建一个包含多页 tiff 文件所有帧的 base64 字符串?

    使用已知的转换方法将多页 tiff 文件转换为 base64 字符串似乎只包含其中的一个页面 我从本地磁盘获取多页 tiff 文件 Image multiPageImage Image FromFile fileName 将其转换为 bas
  • Objective-C - ARC - NSNumber - 分段错误

    我有一个 Objective C 程序 并且正在使用 ARC 自动引用计数 它在第 23 行抛出分段错误 请参见下面的程序 Question1 为什么会出现分段错误 下面给出的是程序 import
  • 通过在 R 中填充 NA - 使栅格达到相同程度

    我有几个具有不同几何形状 轮廓的裁剪栅格 具体而言 同一田地的几年的空间产量图 但范围有所不同 测量并不总是整个田地的整体 但在某些年份只是其中的一部分 我想计算这些地图的平均值并将它们组合成一个平均值栅格 然而 这确实意味着 假设 5 层
  • 将现有数据库放置在我的项目中的何处

    我想在基于 Android Android Studio 的项目中使用现有的 sqlite 数据库 我在 Google 上进行了搜索 有人建议将其放在 资产 文件夹中 但是在项目结构中没有这样的文件夹 项目中的文件夹结构如下 res 可绘制
  • Angularjs - ng-click 函数与指令

    我无法决定在以下情况下使用哪种方法 我试图在点击按钮时发出警报 我可以使用两种方法来做到这一点 哪个是最佳实践 请告诉我为什么 Method 1 div div
  • 通过蓝牙将字符串从作为客户端的 PC 发送到作为服务器的移动设备

    我需要通过蓝牙将字符串从 PC 传输到 Android 移动设备的帮助 Android 移动设备应充当服务器并在设备屏幕上显示字符串消息 作为客户端的 PC 应该将字符串发送到移动设备 我希望服务器对提取的字符串 通过蓝牙传输 做出反应 这