问题
I am creating a Android Application
that will have a Menu
containing two options:
Chat
viaBluetooth
.Transfer Files
viaBluetooth
.Config
containing controls to turnBluetooth
OFF
andON
, andVisibility
OFF
andON
1) I have made the Chat and now it works, but the method that i have used i dont think so that is totally right.
I have a button "Server" and another button "Client" one mobile phone needs to click server and wait to the other click client and connect to it.
There is another way to Chat instead of this way?
I can provide code as possible, but i dont know what part of code that i need to provide, because i cant post all the whole code here, can i? its too broad.
2) I want to Transfer Files using the samme connection that i used on the Chat.
Can i do that?
回答1:
Maybe the android bluetooth tutorial can help you
And this tutorial say how you can send messages and files between devices: Bluetooth Data Transfer
But, basically: You need add this permission to your manifest.xml file:
<uses-permission android:name="android.permission.BLUETOOTH"/>
And your on create event could looks like: (But all the codes are in the tutorial)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.txDevice);
MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
ba = BluetoothAdapter.getDefaultAdapter();
if(!ba.isEnabled()){
Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivity(intent);
Intent intent1 = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
startActivity(intent1);
}
回答2:
Google has provided a sample app to demonstrate the Bluetooth chat. You can refer it.
https://android.googlesource.com/platform/development/+/25b6aed7b2e01ce7bdc0dfa1a79eaf009ad178fe/samples/BluetoothChat
It is fully functional bluetooth chat application.
And to activate and deactivate bluetooth you can use this code
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter.isEnabled()) {
//deatctivate bluetooth
mBluetoothAdapter.disable();
}
//Activate bluetooth
mBluetoothAdapter.enable();
来源:https://stackoverflow.com/questions/19960663/how-to-connect-to-phone-and-send-files-chat-messages-via-bluetooth