三种近场通信技术
三种近场通信的技术:WIFI、蓝牙、NFC。
近场通信技术特点
1、WIFI WIFI是一个创建于IEEE802.11标准的无线局域网技术,数据安全性能比蓝牙差一些。 工作频段:2.4G/5G 有效传输距离:10米至数公里 传输速度:<1.3Gbps 连接方式:单点对多点 功耗:较高
2、蓝牙 蓝牙技术是一种无线数据和语音通信开放的全球规范,它是基于低成本的近距离无线连接,为固定和移动设备建立通信环境的一种特殊的近距离无线技术连接。 工作频段:2.4G 有效传输距离:10米内 传输速度:<24Mbps 连接方式:单点对多点 功耗:低
3、NFC NFC全称为近场通信技术。NFC是在非接触式射频识别(RFID)技术的基础上,结合无线互连技术研发而成,比蓝牙连接更简单。 工作频段:13.56Mhz 有效传输距离:20厘米内 传输速度:<424kbps 连接方式:点对点 功耗:很低
未来应用场景
1、WIFI 从上面的技术特点可以看出,WIFI速率远远快于其他两种技术,WiFi技术具有高速率的特点,可以用于对速率要求高的方面,例如物联网、虚拟现实、超高清视频等应用领域。再加之现在又有新兴技术5G,以它为辅它可以可改善WiFi在户外场景无法实现大量设备远距传输的问题。 它的有效传输距离远于其他两种技术,可用于智能家居、无人机控制等方面。
2、蓝牙 首先个人对蓝牙的未来很看好。蓝牙的覆盖率广,手机上、笔记本电脑、平板上等都有蓝牙。 蓝牙的应用于蓝牙耳机、蓝牙音箱、无线鼠标、无线数据传输、车载设备通信等场景。蓝牙的无线连接相对于WIFI更加便利,可用于可穿戴设备。并且蓝牙在数据传输上的优势主要在于低功耗,在未来也会处于一个热门状态。除了我们熟悉的蓝牙功能外,蓝牙还具有音频传输、数据传输、位置服务和设备网络的功能。
3、NFC NFC技术它可以利用芯片来读取信息,达到数据交换的目的。主要应用场景:手机支付、门禁、信用卡、AndroidBeam。 它不同与WIFI、蓝牙的单点对多点的连接方式,具有点对点的特点,可以用于交换名片等具有需要一对一的方面。开始我觉得它的距离太近具有缺陷,但是对于一些读取信息操作来说,近距离读取具有一定的可控性,更加安全,例如支付等操作。
蓝牙通信
设计
简易版的蓝牙通信。 客户端: 1、创建客户端蓝牙 2、创建连接 3、读写数据 4、关闭 服务端: 1、创建服务端蓝牙 2、创建监听listen(蓝牙忽略, 蓝牙没有此监听,而是通过whlie(true)死循环来一直监听的) 3、连接 4、读写数据 5、关闭
部分代码
public class BluetoothChat extends AppCompatActivity{
public static final int MESSAGE_STATE_CHANGE = 1;
public static final int MESSAGE_READ = 2;
public static final int MESSAGE_WRITE = 3;
public static final int MESSAGE_DEVICE_NAME = 4;
public static final int MESSAGE_TOAST = 5;
public static final String DEVICE_NAME = "device_name";
public static final String TOAST = "toast";
private static final int REQUEST_CONNECT_DEVICE = 1;
private static final int REQUEST_ENABLE_BT = 2;
private TextView mTitle;
private ListView mConversationView;
private EditText mOutEditText;
private Button mSendButton;
private String mConnectedDeviceName = null;
private ArrayAdapter<String> mConversationArrayAdapter;
private StringBuffer mOutStringBuffer;
private BluetoothAdapter mBluetoothAdapter = null;
private ChatService mChatService = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().hide();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
}
}
Toolbar toolbar = findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.option_menu);
toolbar.setOnMenuItemClickListener(new MyMenuItemClickListener());
mTitle = findViewById(R.id.title_left_text);
mTitle.setText(R.string.app_name);
mTitle = findViewById(R.id.title_right_text);
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter == null) {
Toast.makeText(this, "蓝牙不可用", Toast.LENGTH_LONG).show();
finish();
return;
}
if (!mBluetoothAdapter.isEnabled()) {
Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableIntent, REQUEST_ENABLE_BT);
} else {
if (mChatService == null) {
setupChat();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults.length>0){
if(grantResults[0]!=PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "未授权,蓝牙搜索功能将不可用!", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public synchronized void onResume() {
super.onResume();
if (mChatService != null) {
if (mChatService.getState() == ChatService.STATE_NONE) {
mChatService.start();
}
}
}
private void setupChat() {
mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.device_list);
mConversationView = findViewById(R.id.in);
mConversationView.setAdapter(mConversationArrayAdapter);
mOutEditText = findViewById(R.id.edit_text_out);
mOutEditText.setOnEditorActionListener(mWriteListener);
mSendButton = findViewById(R.id.button_send);
mSendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView view = findViewById(R.id.edit_text_out);
String message = view.getText().toString();
sendMessage(message);
}
});
mChatService = new ChatService(this, mHandler);
mOutStringBuffer = new StringBuffer("");
}
@Override
public void onDestroy() {
super.onDestroy();
if (mChatService != null)
mChatService.stop();
}
private void ensureDiscoverable() {
if (mBluetoothAdapter.getScanMode() != BluetoothAdapter.SCAN_MODE_CONNECTABLE_DISCOVERABLE) {
Intent discoverableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
Toast.makeText(this, "已经设置本机蓝牙设备的可见性,对方可搜索了。", Toast.LENGTH_SHORT).show();
}
}
private void sendMessage(String message) {
if (mChatService.getState() != ChatService.STATE_CONNECTED) {
Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();
return;
}
if (message.length() > 0) {
byte[] send = message.getBytes();
mChatService.write(send);
mOutStringBuffer.setLength(0);
mOutEditText.setText(mOutStringBuffer);
}
}
private TextView.OnEditorActionListener mWriteListener = new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_NULL && event.getAction() == KeyEvent.ACTION_UP) {
String message = view.getText().toString();
sendMessage(message);
}
return true;
}
};
private final Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_STATE_CHANGE:
switch (msg.arg1) {
case ChatService.STATE_CONNECTED:
mTitle.setText(R.string.title_connected_to);
mTitle.append(mConnectedDeviceName);
mConversationArrayAdapter.clear();
break;
case ChatService.STATE_CONNECTING:
mTitle.setText(R.string.title_connecting);
break;
case ChatService.STATE_LISTEN:
case ChatService.STATE_NONE:
mTitle.setText(R.string.title_not_connected);
break;
}
break;
case MESSAGE_WRITE:
byte[] writeBuf = (byte[]) msg.obj;
String writeMessage = new String(writeBuf);
mConversationArrayAdapter.add("我: " + writeMessage);
break;
case MESSAGE_READ:
byte[] readBuf = (byte[]) msg.obj;
String readMessage = new String(readBuf, 0, msg.arg1);
mConversationArrayAdapter.add(mConnectedDeviceName + ": "+ readMessage);
break;
case MESSAGE_DEVICE_NAME:
mConnectedDeviceName = msg.getData().getString(DEVICE_NAME);
Toast.makeText(getApplicationContext(),"链接到 " + mConnectedDeviceName, Toast.LENGTH_SHORT).show();
break;
case MESSAGE_TOAST:
Toast.makeText(getApplicationContext(),
msg.getData().getString(TOAST), Toast.LENGTH_SHORT).show();
break;
}
}
};
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CONNECT_DEVICE:
if (resultCode == Activity.RESULT_OK) {
String address = data.getExtras().getString(DeviceList.EXTRA_DEVICE_ADDRESS);
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
mChatService.connect(device);
} else if (resultCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "未选择任何好友!", Toast.LENGTH_SHORT).show();
}
break;
case REQUEST_ENABLE_BT:
if (resultCode == Activity.RESULT_OK) {
setupChat();
} else {
Toast.makeText(this, R.string.bt_not_enabled_leaving, Toast.LENGTH_SHORT).show();
finish();
}
}
}
private class MyMenuItemClickListener implements Toolbar.OnMenuItemClickListener {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.scan:
Intent serverIntent = new Intent(BluetoothChat.this, DeviceList.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE);
return true;
case R.id.discoverable:
ensureDiscoverable();
return true;
case R.id.back:
finish();
System.exit(0);
return true;
}
return false;
}
}
}
结果截图
源代码
https://gitee.com/muxinji/android.git
|