mirror of
https://github.com/hackku21/nearby_connections.git
synced 2024-10-27 19:14:01 +00:00
fixed file rename bug
This commit is contained in:
parent
cd7e9bc63d
commit
598544d7d7
@ -29,8 +29,9 @@ Add these to AndroidManifest.xml
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
|
||||
<!-- Optional: only required for FILE payloads -->
|
||||
<!-- Optional: only required for FILE payloads-->
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||
```
|
||||
Since ACCESS_FINE_LOCATION and READ_EXTERNAL_STORAGE is considered to be dangerous system permissions, in addition to adding them to your manifest, you must request these permissions at runtime.
|
||||
|
||||
|
@ -62,21 +62,36 @@ public class NearbyConnectionsPlugin implements MethodCallHandler {
|
||||
public void onMethodCall(MethodCall call, final Result result) {
|
||||
|
||||
switch (call.method) {
|
||||
case "checkPermissions":
|
||||
case "checkLocationPermission":
|
||||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_FINE_LOCATION)
|
||||
!= PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION)
|
||||
!= PackageManager.PERMISSION_GRANTED) {
|
||||
result.success(false);
|
||||
} else {
|
||||
== PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(activity, Manifest.permission.ACCESS_COARSE_LOCATION)
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
result.success(true);
|
||||
} else {
|
||||
result.success(false);
|
||||
}
|
||||
break;
|
||||
case "askPermissions":
|
||||
case "askLocationPermission":
|
||||
ActivityCompat.requestPermissions(activity,
|
||||
new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION},
|
||||
0);
|
||||
result.success(null);
|
||||
break;
|
||||
case "checkExternalStoragePermission":
|
||||
if (ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_EXTERNAL_STORAGE)
|
||||
== PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)
|
||||
== PackageManager.PERMISSION_GRANTED) {
|
||||
result.success(true);
|
||||
} else {
|
||||
result.success(false);
|
||||
}
|
||||
break;
|
||||
case "askExternalStoragePermission":
|
||||
ActivityCompat.requestPermissions(activity,
|
||||
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},
|
||||
0);
|
||||
result.success(null);
|
||||
break;
|
||||
case "stopAdvertising":
|
||||
Log.d("nearby_connections", "stopAdvertising");
|
||||
Nearby.getConnectionsClient(activity).stopAdvertising();
|
||||
@ -358,7 +373,7 @@ public class NearbyConnectionsPlugin implements MethodCallHandler {
|
||||
assert bytes != null;
|
||||
args.put("bytes", bytes);
|
||||
}
|
||||
else if (payload.getType() == Payload.Type.BYTES) {
|
||||
else if (payload.getType() == Payload.Type.FILE) {
|
||||
args.put("filePath", payload.asFile().asJavaFile().getAbsolutePath());
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,9 @@
|
||||
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
|
||||
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
|
||||
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
|
||||
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
|
||||
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
|
||||
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
|
||||
calls FlutterMain.startInitialization(this); in its onCreate method.
|
||||
In most cases you can leave this as-is, but you if you want to provide
|
||||
|
@ -33,7 +33,7 @@ class Body extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _MyBodyState extends State<Body> {
|
||||
final String userName = Random().nextInt(1000).toString();
|
||||
final String userName = Random().nextInt(10000).toString();
|
||||
final Strategy strategy = Strategy.P2P_STAR;
|
||||
String cId = "0"; //currently connected device ID
|
||||
File tempFile; //stores the file being transferred
|
||||
@ -42,14 +42,17 @@ class _MyBodyState extends State<Body> {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: Column(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: ListView(
|
||||
children: <Widget>[
|
||||
Text("Permissions",),
|
||||
Wrap(
|
||||
children: <Widget>[
|
||||
RaisedButton(
|
||||
child: Text("checkPermission"),
|
||||
child: Text("checkLocationPermission"),
|
||||
onPressed: () async {
|
||||
if (await Nearby().checkPermissions()) {
|
||||
if (await Nearby().checkLocationPermission()) {
|
||||
Scaffold.of(context).showSnackBar(SnackBar(
|
||||
content: Text("Location permissions granted :)")));
|
||||
} else {
|
||||
@ -59,13 +62,34 @@ class _MyBodyState extends State<Body> {
|
||||
},
|
||||
),
|
||||
RaisedButton(
|
||||
child: Text("askPermission"),
|
||||
child: Text("askLocationPermission"),
|
||||
onPressed: () async {
|
||||
await Nearby().askPermission();
|
||||
await Nearby().askLocationPermission();
|
||||
},
|
||||
),
|
||||
RaisedButton(
|
||||
child: Text("checkExternalStoragePermission"),
|
||||
onPressed: () async {
|
||||
if (await Nearby().checkExternalStoragePermission()) {
|
||||
Scaffold.of(context).showSnackBar(SnackBar(
|
||||
content:
|
||||
Text("External Storage permissions granted :)")));
|
||||
} else {
|
||||
Scaffold.of(context).showSnackBar(SnackBar(
|
||||
content: Text(
|
||||
"External Storage permissions not granted :(")));
|
||||
}
|
||||
},
|
||||
),
|
||||
RaisedButton(
|
||||
child: Text("askExternalStoragePermission"),
|
||||
onPressed: () async {
|
||||
await Nearby().askExternalStoragePermission();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
Divider(),
|
||||
Text("User Name: " + userName),
|
||||
Wrap(
|
||||
children: <Widget>[
|
||||
@ -83,7 +107,7 @@ class _MyBodyState extends State<Body> {
|
||||
showSnackbar(status);
|
||||
},
|
||||
onDisconnected: (id) {
|
||||
showSnackbar("Disconnected: "+id);
|
||||
showSnackbar("Disconnected: " + id);
|
||||
},
|
||||
);
|
||||
showSnackbar(a);
|
||||
@ -169,6 +193,8 @@ class _MyBodyState extends State<Body> {
|
||||
await Nearby().stopAllEndpoints();
|
||||
},
|
||||
),
|
||||
Divider(),
|
||||
Text("Sending Data",),
|
||||
RaisedButton(
|
||||
child: Text("Send Random Bytes Payload"),
|
||||
onPressed: () async {
|
||||
@ -195,6 +221,7 @@ class _MyBodyState extends State<Body> {
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@ -261,10 +288,8 @@ class _MyBodyState extends State<Body> {
|
||||
showSnackbar(endid + ": FAILED to transfer file");
|
||||
} else if (payloadTransferUpdate.status ==
|
||||
PayloadStatus.SUCCESS) {
|
||||
print(
|
||||
showSnackbar(
|
||||
"success, total bytes = ${payloadTransferUpdate.totalBytes}");
|
||||
showSnackbar(endid +
|
||||
": SUCCESS in file transfer (file is un-named in downloads) ");
|
||||
|
||||
if (map.containsKey(payloadTransferUpdate.id)) {
|
||||
//rename the file now
|
||||
|
@ -149,15 +149,29 @@ class Nearby {
|
||||
///
|
||||
/// retruns true/false based on location permissions.
|
||||
/// Discovery cannot be started with insufficient permission
|
||||
Future<bool> checkPermissions() async => await _channel.invokeMethod(
|
||||
'checkPermissions',
|
||||
Future<bool> checkLocationPermission() async => await _channel.invokeMethod(
|
||||
'checkLocationPermission',
|
||||
);
|
||||
|
||||
/// Convinience method
|
||||
///
|
||||
/// Asks location permission
|
||||
Future<void> askPermission() async => await _channel.invokeMethod(
|
||||
'askPermissions',
|
||||
Future<void> askLocationPermission() async => await _channel.invokeMethod(
|
||||
'askLocationPermission',
|
||||
);
|
||||
|
||||
/// Convinience method
|
||||
///
|
||||
/// retruns true/false based on external storage permissions.
|
||||
Future<bool> checkExternalStoragePermission() async => await _channel.invokeMethod(
|
||||
'checkExternalStoragePermission',
|
||||
);
|
||||
|
||||
/// Convinience method
|
||||
///
|
||||
/// Asks external storage permission, required for file
|
||||
Future<void> askExternalStoragePermission() async => await _channel.invokeMethod(
|
||||
'askExternalStoragePermission',
|
||||
);
|
||||
|
||||
/// Start Advertising
|
||||
|
Loading…
Reference in New Issue
Block a user