android wifi(WAP/PSK加密)蓝牙设备的连接
2014-04-25 11:29
465 查看
(正在上班,待续)
WIFI配置权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
蓝牙连接的配置权限
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
WIFI连接
其实wifi的连接参数可以在文件里面找到(需要手机root),
进入cmd命令提示符,adb shell进入 data/misc/wifi,查看wpa_supplicant.conf这个文件,使用cat wpa_supplicant.conf这个指令,我们可以看到之前连接的一些信息
一:蓝牙连接聊天
activity_main.xml
WIFI配置权限
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
蓝牙连接的配置权限
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
WIFI连接
其实wifi的连接参数可以在文件里面找到(需要手机root),
进入cmd命令提示符,adb shell进入 data/misc/wifi,查看wpa_supplicant.conf这个文件,使用cat wpa_supplicant.conf这个指令,我们可以看到之前连接的一些信息
一:蓝牙连接聊天
package com.liu.blutoolth; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothServerSocket; import android.bluetooth.BluetoothSocket; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; @SuppressLint("HandlerLeak") public class MainActivity extends Activity implements OnClickListener { private Button searchBtn; // 搜索按钮 private TextView showList; // 蓝牙数据 private BluetoothAdapter ma; // 蓝牙 private IntentFilter mFilter; private Button serverBtn; // 作为服务器 private Button clientBtn; // 作为客户端 private BluetoothServerSocket sServerSocket = null; private static BluetoothDevice mDevice; // 客户端要连接的设备 private ServerThread server; // 服务端线程 private ConnectServer connect; // 客户端线程 private static UUID uuid = null; // 双方的UUID private EditText uuidText; // UUID(后四位) private BluetoothSocket clientSocket; // 客户端连接端口 private BluetoothSocket serverSocket; // 服务端连接端口 private EditText sendText; private Button send; private Handler hand; private boolean isServer = true; private OutputStream outClient; private InputStream inClient; private final BroadcastReceiver br = new BroadcastReceiver() { @SuppressLint("NewApi") @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(BluetoothDevice.ACTION_FOUND)) { BluetoothDevice device = intent .getParcelableExtra(BluetoothDevice.EXTRA_DEVICE); if ("C230t".trim().equals(device.getName())) { //这里的"C230t"是你想要连接的蓝牙设备的名称,需要手动修改 mDevice = device; // 获取对方设备 ma.cancelDiscovery(); } StringBuilder sb = new StringBuilder(); sb.append("name:" + device.getName() + " address:" + device.getAddress() + "\n"); showList.append(sb); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { searchBtn = (Button) findViewById(R.id.search); showList = (TextView) findViewById(R.id.showlist); serverBtn = (Button) findViewById(R.id.server); clientBtn = (Button) findViewById(R.id.client); uuidText = (EditText) findViewById(R.id.uuid); sendText = (EditText) findViewById(R.id.sent_text); send = (Button) findViewById(R.id.send); serverBtn.setOnClickListener(this); clientBtn.setOnClickListener(this); mFilter = new IntentFilter(BluetoothDevice.ACTION_FOUND); registerReceiver(br, mFilter); hand = new Handler() { @Override public void handleMessage(Message msg) { StringBuilder sb = new StringBuilder(); sb.append((String) msg.obj + "\n"); showList.append(sb); if (((String) msg.obj).equalsIgnoreCase("wifi")) { // Intent intent = new Intent(); /* * intent.setAction("com.liu.wifi.mainactivity"); * * startActivity(intent); */ LayoutInflater inflate = getLayoutInflater(); View view = inflate.inflate(R.layout.wifi_dialog, null); Builder wifiDialog = new AlertDialog.Builder( MainActivity.this); wifiDialog.setTitle("title"); wifiDialog.setView(view); wifiDialog.setPositiveButton("ok", null); wifiDialog.setNegativeButton("cancel", null); wifiDialog.show(); } } }; send.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if (isServer) { ServerSendMessage(); } else { ClientSendMessage(); } } }); showList.setText("DEVICE"); ma = BluetoothAdapter.getDefaultAdapter(); searchBtn.setOnClickListener(new OnClickListener() { // 搜索按钮 @Override public void onClick(View v) { ma.cancelDiscovery(); ma.startDiscovery(); Intent canFind = new Intent( BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE); canFind.putExtra( BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300); startActivity(canFind); } }); if (!ma.isEnabled()) { Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(intent, 1); } } private OutputStream outServer; private void ServerSendMessage() { try { if (!sendText.getText().toString().trim().equals("")) { outServer .write(sendText.getText().toString().trim().getBytes()); } } catch (IOException e) { e.printStackTrace(); } } protected void ClientSendMessage() { try { if (!sendText.getText().toString().trim().equals("")) { outClient .write(sendText.getText().toString().trim().getBytes()); } } catch (IOException e) { e.printStackTrace(); try { outClient.close(); } catch (IOException e1) { e1.printStackTrace(); } } } @Override public void onClick(View v) { switch (v.getId()) { case R.id.server: isServer = true; clientBtn.setVisibility(View.INVISIBLE); uuid = UUID.fromString("00035201-0000-1000-8000-00805F92" + uuidText.getText().toString().trim()); startServer(); break; case R.id.client: isServer = false; serverBtn.setVisibility(View.INVISIBLE); uuid = UUID.fromString("00035201-0000-1000-8000-00805F92" + uuidText.getText().toString().trim()); startConnect(); break; default: break; } } private void startConnect() { if (uuid == null) { } else { if (MainActivity.mDevice != null) { connect = new ConnectServer(mDevice, uuid); connect.start(); } } } private void startServer() { if (server == null) { server = new ServerThread(); server.start(); } } private InputStream inServer; // 服务器接收 // 服务端等待线程 private class ServerThread extends Thread { public ServerThread() { try { sServerSocket = ma.listenUsingRfcommWithServiceRecord("bt", uuid); //这里的 "bt"是本地设备名,可修改 } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { ma.cancelDiscovery(); while (true) { try { serverSocket = sServerSocket.accept(); outServer = serverSocket.getOutputStream(); inServer = serverSocket.getInputStream(); if (serverSocket != null) { sServerSocket.close(); break; } } catch (IOException e) { e.printStackTrace(); try { if (sServerSocket != null) { sServerSocket.close(); } } catch (IOException e1) { e1.printStackTrace(); } } } } } // 客户端连接线程 private class ConnectServer extends Thread { public ConnectServer(BluetoothDevice device, UUID uuid) { try { clientSocket = device.createRfcommSocketToServiceRecord(uuid); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { ma.cancelDiscovery(); try { clientSocket.connect(); inClient = clientSocket.getInputStream(); outClient = clientSocket.getOutputStream(); new handClientThread().start(); } catch (IOException e) { e.printStackTrace(); try { clientSocket.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } @Override protected void onDestroy() { super.onDestroy(); unregisterReceiver(br); } // 处理客户端发来的数据 private class handClientThread extends Thread { @Override public void run() { byte[] buffer = new byte[1024]; int size = 0; while (true) { try { if (isServer) { size = inServer.read(buffer); String str = new String(buffer, 0, size, "UTF-8"); Message msg = new Message(); msg.obj = str; hand.sendMessage(msg); } else { size = inClient.read(buffer); String str = new String(buffer, 0, size, "UTF-8"); Message msg = new Message(); msg.obj = str; hand.sendMessage(msg); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); break; } } } } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:text="@string/search" /> <TextView android:id="@+id/showlist" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/uuid" /> <Button android:id="@+id/server" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/search" android:text="@string/server" /> <Button android:id="@+id/client" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/search" android:text="@string/client" /> <EditText android:id="@+id/uuid" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginTop="22dp" android:layout_below="@id/server" android:hint="@string/uuid" android:ems="10" > <requestFocus /> </EditText> <EditText android:id="@+id/sent_text" android:hint="@string/sent_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignLeft="@+id/send" /> <Button android:id="@+id/send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" android:layout_marginLeft="29dp" android:text="@string/send" /> </RelativeLayout>
相关文章推荐
- android蓝牙开发 蓝牙设备的查找和连接
- adb 用 wifi 连接 android 设备
- Android 蓝牙设备的查找和连接
- Android和iOS如何连接指定的蓝牙设备
- wifi连接电脑和android设备
- Appium:通过wifi连接Android设备
- Android中连接蓝牙设备时遇到createRfcommSocketToServiceRecord的UUID问题和BluetoothSocket的connect失败
- adb通过wifi连接android设备
- Android 扫描附近的蓝牙设备并连接蓝牙音响的示例
- Android蓝牙开发之连接设备
- adb wifi连接android设备
- 4000 Android 如何获取已连接的蓝牙设备
- Android开发之蓝牙连接与配对设备
- adb通过wifi连接android设备的方法
- Android蓝牙设备的配对,连接,搜索
- Android开发笔记(一百六十二)蓝牙设备的连接与配对
- Android官方开发文档Training系列课程中文版:连接无线设备之通过WIFI创建P2P连接
- Android Wifi --自动连接指定SSID(各种加密方式均可)
- Android 不连接蓝牙设备获取信号强度
- Android 蓝牙获得连接设备的Mac地址和名称