- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
很多天以来,我一直在研究与我的问题相关的许多示例和问题,但没有任何帮助!
瞄准
pc(windows) 和 android 之间的双向通信(比如将消息传递给 pc 并从 pc 确认)
到目前为止我做了什么
我可以使用蓝牙从我的 android 设备向 pc(windows) 发送字符串。
这是我使用的代码
private String sendFile(String mac_address, String device_name) {
// TODO Auto-generated method stub
String result="";
btAdapter = BluetoothAdapter.getDefaultAdapter();
BluetoothDevice device = btAdapter.getRemoteDevice(mac_address);
Log.d("BT_SENDING_FILE",device_name+" "+mac_address);
try {
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
btSocket = (BluetoothSocket) m.invoke(device, 1);
if(!btSocket.isConnected()){
Log.d(" is connected Status",""+btSocket.isConnected());
// device.createInsecureRfcommSocketToServiceRecord(UUID);
}
btAdapter.cancelDiscovery();
try{
btSocket.connect();
}catch(IOException e){
btSocket =(BluetoothSocket) device.getClass().getMethod("createRfcommSocket", new Class[] {int.class}).invoke(device,1);
btSocket.connect();
Log.d("fall back","in catch clause");
}
byte[] msgBuffer = stringToSend.getBytes();
outStream = btSocket.getOutputStream();
outStream.write(msgBuffer);
if (outStream != null) {
outStream.flush();
}
result = "sent";
outStream.close();
btSocket.close();
Log.d("BLUETOOTH","Closing Socket");
} catch (Exception e) {
System.out.println(e);
Log.d("BLUETOOTH","exception while sending through bluetooth");
result = "failed";
e.getLocalizedMessage();
} finally{}
return result;
}
运行良好,没有任何问题。
问题
但我无法从 PC 接收任何字符串到 Android 设备。我尝试了很多东西
我试过了
BluetoothSocket socket = null;
BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();
while (true) {
try {
// Creating a new listening server socket
Log.d("BT", ".....Initializing RFCOMM SERVER....");
mmServerSocket = mAdapter.listenUsingInsecureRfcommWithServiceRecord("Bluetooth", MY_UUID);
// This is a blocking call and will only return on a
// successful connection or an exception
Log.d("Socket","listening...");
socket = mmServerSocket.accept(120000);
} catch (Exception e) {
}
try {
Log.d("Socket", "Closing Server Socket.....");
mmServerSocket.close();
InputStream tmpIn = null;
OutputStream tmpOut = null;
// Get the BluetoothSocket input and output streams
tmpIn = socket.getInputStream();
tmpOut = socket.getOutputStream();
Log.d("BT","IO stream init");
DataInputStream mmInStream = new DataInputStream(tmpIn);
DataOutputStream mmOutStream = new DataOutputStream(tmpOut);
// 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
StringBuilder sb = new StringBuilder();
while((ch = mmInStream.read())!= -1)
sb.append((char)ch);
String res = sb.toString();
Log.d("BT",res);
tv_response.setText(res));
} catch (Exception e) {
//catch your exception here
e.printStackTrace();
Log.d("Exception","some thing went wrong");
e.getLocalizedMessage();
}
}
}
我在 Windows 中使用 Blue Cove。 我已经完成了蓝牙聊天示例项目但是什么都不懂
感谢您的帮助
谢谢
最佳答案
我尝试了以下代码并且成功了。可能对你有帮助
BluetoothSocket socket = null;
while (true) {
try {
Log.i(ACCEPT_TAG, "Listening for a connection...");
socket = mServerSocket.accept();
Log.i(ACCEPT_TAG, "Connected to " + socket.getRemoteDevice().getName());
} catch (IOException e) {
break;
}
// If a connection was accepted
if (socket != null) {
// Do work to manage the connection (in a separate thread)
try {
// Read the incoming string.
String buffer;
DataInputStream in = new DataInputStream(socket.getInputStream());
buffer = in.readUTF();
Intent i = new Intent(MESSAGE_RECEIVED_INTENT);
i.putExtra("Message", String.format("%s", buffer));
Log.d("BT response is",buffer);
c.sendBroadcast(i);
} catch (IOException e) {
Log.e(ACCEPT_TAG, "Error obtaining InputStream from socket");
e.printStackTrace();
}
try {
mServerSocket.close();
} catch (IOException e) { }
break;
}
}
确保您的设备对所有人可见,即扫描模式为 BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE
否则使用此代码
public void requestBTDiscoverable() {
Intent i = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
i.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivityForResult(i, REQ);
int result = 0;
this.onActivityResult(REQ, result, i);
Log.i(TAG, "Bluetooth discoverability enabled");
}
关于android - 通过蓝牙将字符串从android传输到pc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26056698/
我的 PC 上有一个服务器应用程序,它读取 jpg 文件并通过套接字将其发送到 Android 设备。问题是,当Android设备接收到字节数组时,无法将其转换为位图。我创建了一个 PC 应用程序来接
我创建了一个 JavaFX 应用程序。现在我想确保它不被复制到其他计算机上。更详细地说,我将该应用程序出售给一位客户,安装该应用程序后,我需要确保其不被从一台计算机复制到另一台计算机。 如何防止他人复
我构建了一个使用 Dynamic 关键字的程序。 在我的代码中,我这样做了: public void OnNext(ExpandoObject value) { dynamic expando
如何通过USB在两台PC之间进行通信?我想要一个程序通过 USB 端口将数字发送到另一台 PC,另一个程序将在该 PC 上显示这些数字。我觉得这是不可能的,因为 PC 是主机而不是设备,但 USB 真
我的代码在 virtualbox 中完美运行,但在真实 PC 上启动时却无法运行(从 BIOS 检测为 USB 硬盘驱动器的 USB 笔驱动器)。 在虚拟框中;该代码将磁盘的第 2 和第 3 扇区读取
在开发 PC HD 损坏后,我正在考虑让我的开发环境完全基于虚拟 PC。 核心项目是:- XP Pro 32- IIS- VS2003- VS2008- SQL Server 2005- Office
我目前使用的是 Windows Server 2008 Standard 并且有几台 Hyper V 机器。这些是开发 VM,我现在想切换回 Vista x64,因为我缺少 Aero。 我知道 Win
我使用 Virtual PC 来创建新的环境来测试我的安装程序。但我一定是做错了什么,因为内部装有 Vista 或 XP 的 VPC 镜像占用了大约 15GB 的磁盘空间(包括安装在其中的 VS200
大家好,我正在调试一些 CS 程序,为了查看应用程序在慢速互联网中的性能,我尝试了很多不同的方法。然而最好的是服务器端和客户端在同一台电脑上——我的服务器端和客户端的调试环境是在一台电脑上设置的。 所
我有兴趣制作一个将字符串从一台计算机传输到另一台计算机的应用程序。我对 TCP 或 UDP 通信感兴趣。我已经实现了 UDP,但它似乎能够发送最多 512 字节的数据/数据包。在两端实现数据包拆分和连
在为 Pocket PC 平台开发软件时,我一直很高兴地使用 Microsoft 随 Visual Studio 提供的 Pocket PC 模拟器(并且可以免费下载)。它提供了更快的开发/部署/测试
我想执行一个批处理文件 D:\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\shutdown.bat 这是在我的服务器上 inidsoasrv01 . 我
我目前正在我的开发 PC 上使用 Jenkins。我把它安装在我的开发电脑上,因为我对这个工具的了解有限;所以我在我的开发电脑上对其进行了测试。现在,我对 Jenkins 作为我在构建过程中的长期“合
互联网上有很多示例展示了如何编写一个应用程序,使我们能够通过蓝牙与手机与电脑进行通信。但我想做的是通过蓝牙从另一台电脑控制一台电脑。我正在尝试使用蓝牙库。我在一台电脑上运行服务器,并尝试使用此处的示例
我正在寻找一个基准测试(以及在其他 PC 上的结果),它可以让我了解通过升级我的 PC 可以获得的开发性能提升,而且该基准测试可以用来向我的老板证明升级的合理性。 我使用 Visual Studio
我只在一台 PC 上有异常,在其他 PC 上一切正常,有人知道它是从哪里来的吗? dditional information: Requested Windows Runtime type 'Wind
我想创建一个软件,它可以使用 session 选项进行 pc 到 pc 调用(没有电话)。所有参与者将仅使用该软件。我擅长使用不同的语言、平台和数据库进行编程。但是我以前没有做过这种类型的软件。 我将
我做了这个布局。 template 唯一的问题是宽度问题。它因显示器而异,取决于显示器的宽度。主菜单和标题区域的左右两侧都有空白区域。在页脚的情况下你可以看到同样的情况.. 根据我的显示器宽度,我有
我在我的电脑上编译了一个内核,然后我把它安装在同一台电脑上,它工作正常。我的问题是如何在另一台计算机上安装相同的内核? 最佳答案 您需要复制 vmlinuz-[version number] 和 in
我正在使用 Mysql,我在两个不同的系统中有两个数据库。我希望我的本地数据库与远程数据库同步。这两个架构将具有相同的表和列。 每当远程数据库发生变化时,我的本地数据库中应该发生变化/更新。 怎么做?
我是一名优秀的程序员,十分优秀!