Check Cellphone State : Bluetooth、NetWork
權限
<!-- 藍芽 -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<!-- 網路 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
/**
* 裝置狀態
*/
public class DeviceState {
/** 網路是否連線 */
public static boolean isNetWorkConnection(Context context){
boolean isConnection = false;
ConnectivityManager mConnectivityManager =
(ConnectivityManager) context.getApplicationContext()
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if(mNetworkInfo != null)
if(mNetworkInfo.isConnected())
isConnection = true;
return isConnection;
}
/** 裝置是否支援低功率藍芽 */
public static boolean isSupportBle(){
boolean isSupport = false;
BluetoothAdapter mBluetoothAdapter =
BluetoothAdapter.getDefaultAdapter();
if (mBluetoothAdapter != null)
isSupport = true;
return isSupport;
}
}
參考資料 : 【Android】檢查網路連線狀態 Connectivity Network Active State
歡迎轉載,請註明出處。
沒有留言:
張貼留言