Java 如何通过蓝牙将文件从android发送到计算机
任何人都可以给出一个蓝牙服务器客户端的工作示例(从android到计算机) 我想将数据从我的Android手机传输到C#应用程序。现在,我正在使用下面的android代码,它工作得很好,但它仍然不能将数据传输到我的计算机(可能是无法写入这些数据) 有人能推荐一些代码,可以成功地将消息或文件从客户端Android传输到服务器PC吗Java 如何通过蓝牙将文件从android发送到计算机,java,android,bluetooth,Java,Android,Bluetooth,任何人都可以给出一个蓝牙服务器客户端的工作示例(从android到计算机) 我想将数据从我的Android手机传输到C#应用程序。现在,我正在使用下面的android代码,它工作得很好,但它仍然不能将数据传输到我的计算机(可能是无法写入这些数据) 有人能推荐一些代码,可以成功地将消息或文件从客户端Android传输到服务器PC吗 Button btnSend = null; TextView txtPath = null; Socket s = null; BluetoothAdapter ob
Button btnSend = null;
TextView txtPath = null;
Socket s = null;
BluetoothAdapter objBluetoothAdapter = null;
BluetoothDevice device = null;
BluetoothSocket socket = null;
String strPath = "/sdcard/bluetooth/IMG0245A.jpg";
byte [] buffer = null;
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSend = (Button)findViewById(R.id.send_button);
btnSend.setOnClickListener(this);
}
@Override public void onClick(View arg0) {
// TODO Auto-generated method stub
String address="MY_COMPUTER_BLUETOOTH_ADDRESS";
objBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if(objBluetoothAdapter==null){
Toast.makeText(this, "BT not supported", Toast.LENGTH_LONG);
return;
}
//objBluetoothAdapter.enable();
if(!objBluetoothAdapter.isEnabled()){
Intent enableBT = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(enableBT);
}
try{
device = objBluetoothAdapter.getRemoteDevice(address);
final UUID uuid= UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
try{
File f = new File(strPath);
buffer = new byte[(int)f.length()];
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
bis.read(buffer,0,(int)f.length());
socket = device.createRfcommSocketToServiceRecord(uuid);
Log.d("BT","RF Connection Created"+socket);
//objBluetoothAdapter.startDiscovery();
for(int i=0;i<3;i++){
try{
objBluetoothAdapter.cancelDiscovery();
socket.connect();
Log.d("BT","Socket Connected = "+socket);
break;
}catch (Exception e) {
// TODO: handle exception
Log.d("BT","Socket Connection exception = "+e);
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
Log.d("BT","Connection NOT OK");
}
OutputStream os = socket.getOutputStream();
os.write(buffer);//,0,buffer.length);
os.flush();
os.close();
socket.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this,"exception "+e, Toast.LENGTH_LONG);
当我从android的Bluetooth发送文件时,这段代码很有效,但当我尝试从android代码发送文件时,obxlistener.GetContext()返回空值
谢谢您的帮助您能在问题中输入您所指的代码吗?我输入了我的代码。你能帮我吗?对不起,我帮不了你。我在查看另一个用户所做的编辑时偶然发现了您的问题,发现您提到的代码丢失了。
ObexListenerContext olContexet = obxlistener.GetContext();
ObexListenerRequest olRequest = olContexet.Request;
String[] pathSplits = olRequest.RawUrl.Split('/');
String filename = pathSplits[pathSplits.Length - 1];
olRequest.WriteFile(Path.GetFullPath(Application.StartupPath) + "\\" + filename);