任何人都可以提供蓝牙服务器客户端的实例(从Android到计算机)?
我想将数据从我的Android手机传输到C#应用程序。现在我使用下面的android代码,它运行良好,但它仍然不会将数据传输到我的计算机(可能无法写入这些数据)。
任何人都可以建议一些应该成功将消息或文件从客户端Android传输到服务器PC的代码吗?
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);
这是我的一些C#代码。我使用32feet库
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);
当我从android的蓝牙发送文件时这个代码工作正常但是当我尝试从我的android代码发送文件时,obxlistener.GetContext()返回null值!!!
感谢您的帮助