任何人都可以给出一个蓝牙服务器客户端(从android到计算机)的工作示例,它可以将文件或消息从一端传输到另一端?我使用TCP进行传输,在过去的2天里,蓝牙成功了。
我发现了一些谷歌的文章,但我不能成功的那些。从This教程我尝试自己,但得到异常在onResume()连接失败。
我想将数据从我Android手机转移到我的电脑现在我在下面,因为来自logcat中我看到了连接成功建立和完美的读取数据,其做工不错,代码工作7.At运行Windows,但仍不要将数据传输到我的电脑(可能无法写入这些数据)。
所以我的问题是,我失去的东西还是需要这样的代码,服务器端?或者任何人都可以提出一些代码片段,成功地转移的消息或从客户端的Android到服务器PC文件应注意什么?
我的代码:通过蓝牙将数据从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);
}
}
检查此.. http://digitalhacksblog.blogspot.com/2012/05/android-example-bluetooth-simple-spp.html我用它我的自我,它运作良好.. – 2013-03-08 05:18:44