Android手机之间的蓝牙通信的代码和原理

import android.content.IntentFilter;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.View;

import android.view.ViewGroup;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.BaseAdapter;

import android.widget.EditText;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener {

private BluetoothAdapter bAtapter;

private BluetoothDevice device;

private List<BluetoothDevice> deviceList = new ArrayList<BluetoothDevice>();

private List<String> nameList = new ArrayList<String>();

private ListView mListView;

private BluetoothSocket bs;

private EditText mEditText;

private StringBuilder incoming = new StringBuilder();

private TextView tv;

private BluetoothSocket transferSocket;

@Override

protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);

	setContentView(R.layout.activity_main);

	mEditText = (EditText) findViewById(R.id.et_id);

	tv = (TextView) findViewById(R.id.tv_id);

	initView();

}



public void click(View view) {

	if (transferSocket != null) {

		String str = mEditText.getText().toString();

		sendMessage(transferSocket, "客户端说:" + str + "\n");

	}

}



private void initView() {

	// TODO Auto-generated method stub

	bAtapter = BluetoothAdapter.getDefaultAdapter();

	mListView = (ListView) findViewById(R.id.list_id);

	mListView.setOnItemClickListener(this);

	startDiscovery();

	mListView.setAdapter(baseAdapter);

}



private void startDiscovery() {

	// TODO Auto-generated method stub

	registerReceiver(discoveryRsult, new IntentFilter(

			BluetoothDevice.ACTION_FOUND));



	deviceList.clear();

	nameList.clear();

	bAtapter.startDiscovery();



}



BroadcastReceiver discoveryRsult = new BroadcastReceiver() {



	@Override

	public void onReceive(Context arg0, Intent arg1) {

		// TODO Auto-generated method stub

		nameList.add(arg1.getStringExtra(BluetoothDevice.EXTRA_NAME));

		deviceList.add((BluetoothDevice) arg1

				.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE));

		Log.i("caohaidemo", "nameList:" + nameList.size());

		baseAdapter.notifyDataSetChanged();



	}

};

BaseAdapter baseAdapter = new BaseAdapter() {



	@Override

	public View getView(int arg0, View arg1, ViewGroup arg2) {

		arg1 = LayoutInflater.from(MainActivity.this).inflate(

				R.layout.item, arg2, false);

		TextView tv = (TextView) arg1.findViewById(R.id.item_text);

		tv.setText(nameList.get(arg0));

		return arg1;

	}



	@Override

	public long getItemId(int arg0) {

		// TODO Auto-generated method stub

		return arg0;

	}



	@Override

	public Object getItem(int arg0) {

		// TODO Auto-generated method stub

		return nameList.get(arg0);

	}



	@Override

	public int getCount() {

		// TODO Auto-generated method stub

		return nameList.size();

	}

};



@Override

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

	// TODO Auto-generated method stub

	final BluetoothDevice device = deviceList.get(arg2);

	new Thread() {

		@Override

		public void run() {

			// TODO Auto-generated method stub



			connectToServerSocket(device,

					UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666"));

		}

	}.start();



}







private void connectToServerSocket(BluetoothDevice device, UUID uuid) {

	try {

		BluetoothSocket clientSocket = device

				.createRfcommSocketToServiceRecord(uuid);

		transferSocket = clientSocket;

		// Block until server connection accepted.

		clientSocket.connect();

		runOnUiThread(new Runnable() {



			@Override

			public void run() {

				// TODO Auto-generated method stub

				Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_LONG)

						.show();

			}

		});

		// Start listening for messages.



		listenForMessages(clientSocket, incoming);



		// Add a reference to the socket used to send messages.



	} catch (IOException e) {

		Log.e("BLUETOOTH", "Blueooth client I/O Exception", e);

	}

}



private void sendMessage(BluetoothSocket socket, String message) {

	OutputStream outStream;

	try {

		outStream = socket.getOutputStream();



		// Add a stop character.

		byte[] byteArray = (message + " ").getBytes();

		byteArray[byteArray.length - 1] = 0;



		outStream.write(byteArray);

	} catch (IOException e) {



	}

}



private boolean listening = false;



private void listenForMessages(BluetoothSocket socket,

		final StringBuilder incoming) {

	listening = true;



	int bufferSize = 1024;

	byte[] buffer = new byte[bufferSize];



	try {

		InputStream instream = socket.getInputStream();

		int bytesRead = -1;



		while (listening) {

			bytesRead = instream.read(buffer);

			if (bytesRead != -1) {

				String result = "";

				while ((bytesRead == bufferSize)

						&& (buffer[bufferSize - 1] != 0)) {

					result = result + new String(buffer, 0, bytesRead - 1);

					bytesRead = instream.read(buffer);

				}

				result = result + new String(buffer, 0, bytesRead - 1);

				incoming.append(result);

				Log.i("caohaidemo", "服务器说:" + incoming.toString());

				runOnUiThread(new Runnable() {



					@Override

					public void run() {

						// TODO Auto-generated method stub

						tv.setText(incoming.toString());

					}

				});

			}



		}

		// socket.close();

	} catch (IOException e) {



	} finally {

	}

}

}














![](https://blog.csdn.net/)  

  



2.服务端代码:



package com.example.mybloothserver;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.UUID;

import android.os.Bundle;

import android.app.Activity;

import android.bluetooth.BluetoothAdapter;

import android.bluetooth.BluetoothServerSocket;

import android.bluetooth.BluetoothSocket;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

private EditText mEditText;

private StringBuffer sb;

private TextView tv;

private StringBuilder incoming = new StringBuilder();



@Override

protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);

	setContentView(R.layout.activity_main);

	mEditText = (EditText) findViewById(R.id.et_id);

	sb = new StringBuffer();

	tv = (TextView) findViewById(R.id.tv_id);

	startServerSocket(BluetoothAdapter.getDefaultAdapter());

}



public void click(View view) {



	if (transferSocket != null) {

		String str = mEditText.getText().toString();

		sendMessage(transferSocket, "服务器说:" + str + "\n");

	}



}



private BluetoothSocket transferSocket;



private UUID startServerSocket(BluetoothAdapter bluetooth) {

	UUID uuid = UUID.fromString("a60f35f0-b93a-11de-8a39-08002009c666");

	String name = "bluetoothserver";



	try {

		final BluetoothServerSocket btserver = bluetooth

				.listenUsingRfcommWithServiceRecord(name, uuid);



		Thread acceptThread = new Thread(new Runnable() {

			public void run() {

				try {

					// Block until client connection established.

					final BluetoothSocket serverSocket = btserver.accept();

					transferSocket = serverSocket;

					runOnUiThread(new Runnable() {



						@Override

						public void run() {

							// TODO Auto-generated method stub

							Toast.makeText(MainActivity.this, "连接成功",

									Toast.LENGTH_LONG).show();

						}

					});

					// Start listening for messages.



					listenForMessages(serverSocket, incoming);

					// Add a reference to the socket used to send messages.



				} catch (IOException e) {

					Log.e("BLUETOOTH", "Server connection IO Exception", e);

				}

			}

		});

		acceptThread.start();

	} catch (IOException e) {

		Log.e("BLUETOOTH", "Socket listener IO Exception", e);

	}

	return uuid;

}



/**

 * Listing 16-8: Sending and receiving strings using Bluetooth Sockets

 */

private void sendMessage(BluetoothSocket socket, String message) {

	OutputStream outStream;

	try {

		outStream = socket.getOutputStream();



		// Add a stop character.

		byte[] byteArray = (message + " ").getBytes();

		byteArray[byteArray.length - 1] = 0;



		outStream.write(byteArray);

	} catch (IOException e) {



	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值